连接到Android上已经配对设备时,系统提示您输入蓝牙PIN码 [英] Prompted for Bluetooth PIN when connecting to already paired device on Android

查看:3036
本文介绍了连接到Android上已经配对设备时,系统提示您输入蓝牙PIN码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序连接到支持蓝牙串行端口配置文件的简单设备(SPP)。我能够成功连接并交换数据,但我每次连接用户时被提示输入的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'.

提示符是一个问题,因为如果用户不输入PIN足够快,插座连接超时,用户必须重试。

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.

下面...

@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();
  }
 }

只要用户快速了解有关输入PIN码,连接成功,我可以接收数据。我的直觉是,我错过了设置步骤。我没那么熟悉的BT的具体细节,不过,所以我不知道这可能是那里的设备迫使PIN码被输入问题?

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?

推荐答案

此可能与不保持装置接合,所述远程设备中的问题(意味着存储链接键在随后的连接被使用),这将导致在重新配对,每次和要求PIN码被输入。
Android的通常应该存储的结合信息一旦配对成功。

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.

这篇关于连接到Android上已经配对设备时,系统提示您输入蓝牙PIN码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆