简体   繁体   English

连接到Android上已配对的设备时提示输入蓝牙PIN码

[英]Prompted for Bluetooth PIN when connecting to already paired device on Android

I am developing an Android app to connect to a simple device that supports the bluetooth serial port profile (SPP). 我正在开发一个Android应用程序,以连接到支持蓝牙串行端口配置文件(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. 我能够成功连接和交换数据,但是每次连接时,都会提示用户输入设备的PIN码。

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. 出现提示是一个问题,因为如果用户输入PIN的速度不够快,套接字连接就会超时并且用户必须重试。

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.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. 只要用户快速输入PIN,连接就成功,并且我可以接收数据。 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? 不过,我对BT的细节并不熟悉,因此我不确定这是否是设备强制输入PIN的问题?

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. 对于不保持绑定状态的远程设备,这可能是个问题(意味着存储要在后续连接中使用的链接密钥),这将导致每次重新配对并要求输入PIN。 Android should typically store the bonding information once it has paired successfully. 成功配对后,Android通常应存储绑定信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM