I am developing an Android app to connect to a simple device that supports the bluetooth serial port profile (SPP). I am able to successfully connect and exchange data, but each time I connect the user is prompted to enter the PIN for the device.
In the bluetooth settings I can see that the device is 'paired by not connected'.
The prompt is an issue because if the user is not quick enough in entering the PIN, the socket connect times out and the user must try again.
Relevant bits of code below...
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scanlayout);
...
_Context = this;
_ActivityCreated = true;
_ReceivedText = (TextView)findViewById(R.id._Scan_Results);
_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
_BluetoothDevice = _BluetoothAdapter.getRemoteDevice(_DeviceAddress);
_BusySpinner = ProgressDialog.show(_Context, "", "Connecting to scanner...");
new ConnectToScannerTask().execute(_BluetoothDevice);
}
private final Handler scanReceivedHandler = new Handler()
{
@Override
public void handleMessage(Message message)
{
String receivedText = (String)message.obj;
_ReceivedText.setText(receivedText);
}
};
private class ConnectToScannerTask extends AsyncTask<BluetoothDevice, Void, InputStream>
{
@Override
protected InputStream doInBackground(BluetoothDevice... params)
{
BluetoothDevice device = params[0];
try
{
_Socket = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
_BluetoothAdapter.cancelDiscovery();
_Socket.connect();
return _Socket.getInp开发者_C百科utStream();
}
catch (IOException e)
{
Log.d("ScanActivity.ConnectToScannerTask.doInBackground", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(final InputStream result)
{
_BusySpinner.dismiss();
if (result == null)
{
_ReceivedText.setText("Failed to connect to scanner.");
return;
}
Thread thread = new Thread()
{
@Override
public void run()
{
byte[] buffer = new byte[1024];
try
{
while (_ActivityCreated)
{
Arrays.fill(buffer, (byte) 0);
int bytesRead = result.read(buffer);
if (bytesRead > 0)
{
Message message = scanReceivedHandler.obtainMessage(1, new String(buffer));
message.sendToTarget();
Log.e("ScanActivity", "Received: " + new String(buffer));
}
if (bytesRead < 0)
{
break;
}
}
Message message = scanReceivedHandler.obtainMessage(1, "End of Stream");
message.sendToTarget();
Log.e("ScanActivity", "End of Stream");
}
catch (Exception e)
{
Message message = scanReceivedHandler.obtainMessage(1, "Connection to scanner lost");
message.sendToTarget();
Log.e("ScanActivity", e.getMessage());
}
try
{
_Socket.close();
}
catch (IOException e)
{
Log.e("ScanActivity", e.getMessage());
}
}
};
thread.start();
}
}
As long as the user is quick about entering the PIN, the connect succeeds and I can receive data. My hunch is that I am missing a setup step. I'm not that familiar with the specifics of BT, though, so I am not sure if this might be an issue where the device is forcing the PIN to be entered?
This might be a problem with the remote device that does not keep the device bonded, (meaning storing the link key to be used in subsequent connect) that will result in re-pairing each time and requiring the PIN to be entered. Android should typically store the bonding information once it has paired successfully.
精彩评论