android pin码 经典蓝牙,连接到Android上已经配对设备时,系统提示您输入蓝牙PIN码...

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

{

@Override

protected InputStream doInBackground(BluetoothDevice... params)

{

BluetoothDevice device = params[0];

try

{

_Socket = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);

_BluetoothAdapter.cancelDiscovery();

_Socket.connect();

return _Socket.getInputStream();

}

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值