要将蓝牙设备与嵌入式设备连接,我希望用户点击已发现的设备并自动配对0000
图钉(无需确认,也需要重要的0000
密码),而不是与passkeys建立联系,我没有任何与连接相关的问题
我发现以下代码我不确定它的工作原理也不知道如何在此代码中将设备(手机)与pin="0000"
的设备(手机)配对?
以下是连接代码的一部分。
发现(扫描)设备上的OnItemClicklistner
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
IntentFilter filter = new IntentFilter(
"android.bluetooth.device.action.PAIRING_REQUEST");
registerReceiver(mPairingRequestReceiver, filter);
}
这是我的配对接收器,用于连接0000
pin
private final BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
try {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
//the pin in case you need to accept for an specific pin
String pin="0000";
Log.d(TAG, "Start Auto Pairing. PIN = " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
byte[] pinBytes;
// pinBytes = (""+pin).getBytes("UTF-8");
pinBytes = pin.getBytes();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
device.setPin(pinBytes);
device.setPairingConfirmation(true);
}
} catch (Exception e) {
Log.e(TAG, "Error occurs when trying to auto pair");
e.printStackTrace();
}
}
}
};
注意:创建债券在这里工作。我不想在点击项目时创建债券并用0000针连接到嵌入式设备....谢谢..