与 Nrf UART 的蓝牙配对工作不正常
Bluetooth Pairing with Nrf UART is not working properly
蓝牙配对工作不正常。我正在开发基于蓝牙与 UART 配对的应用程序。在这里,我包含了我的概念,Program.Help 我会解决这个问题。
我的预期结果是如果用户按下“连接”按钮。它应该在没有用户输入和配对请求和 PIN 的确认屏幕的情况下配对。最后设备响应连接。
我的实际结果是确认屏幕和用户输入弹出窗口将打开。之后设备配对。最后,设备没有回复我已连接。
我被那个问题困住了超过 2 天。帮我解决这个问题。
1.在 onstart() 方法中注册 PAIRING
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
this.registerReceiver(mPairingRequestReceiver, filter);
2。 BroadcastReceiver 用于接收配对请求。
private 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", 123456);
//the pin in case you need to accept for an specific pin
byte[] pinBytes;
pinBytes = ("" + pin).getBytes("UTF-8");
device.setPin(pinBytes);
} catch (Exception e) {
Log.e(TAG, "Error occurs when trying to auto pair");
e.printStackTrace();
}
}
}
};
/* 连接设备后我正在创建债券*/
@Override
public void onDeviceConnected(BluetoothDevice device) {
device.createBond();
}
您可以绕过本机蓝牙配对过程并以编程方式与蓝牙外围设备配对。试试这个:
为 BluetoothDevice.ACTION_PAIRING_REQUEST
注册具有最高优先级的接收器。
private void notPaired(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
filter.setPriority(SYSTEM_HIGH_PRIORITY-1);
registerReceiver(mReceiver, filter);
mDevice.createBound();// OR establish connection with the device and read characteristic for triggering the pairing process
getBoundState();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
if(type == BluetoothDevice.PAIRING_VARIANT_PIN){
byte[] pin = "123456".getBytes();
device.setPin(pin);
Log.i("Pairing Process ", "Pairing Key Entered");
abortBroadcast();
}else
Log.i("Pairing Process: ", "Unexected Pairing type");
}
}
};
为确保设备已配对,请为 BluetoothDevice.ACTION_BOND_STATE_CHANGED
注册接收器
private void getBoundState(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(boundStateReciver, filter);
}
private final BroadcastReceiver boundStateReciver= new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
final int d = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,-1);
switch(d){
case BluetoothDevice.BOND_BONDED:
Log.i("Pairing Process ", "Paired successfully");
break;
}
}
}
};
在清单中添加 this 权限
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
蓝牙配对工作不正常。我正在开发基于蓝牙与 UART 配对的应用程序。在这里,我包含了我的概念,Program.Help 我会解决这个问题。
我的预期结果是如果用户按下“连接”按钮。它应该在没有用户输入和配对请求和 PIN 的确认屏幕的情况下配对。最后设备响应连接。
我的实际结果是确认屏幕和用户输入弹出窗口将打开。之后设备配对。最后,设备没有回复我已连接。
我被那个问题困住了超过 2 天。帮我解决这个问题。
1.在 onstart() 方法中注册 PAIRING
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
this.registerReceiver(mPairingRequestReceiver, filter);
2。 BroadcastReceiver 用于接收配对请求。
private 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", 123456);
//the pin in case you need to accept for an specific pin
byte[] pinBytes;
pinBytes = ("" + pin).getBytes("UTF-8");
device.setPin(pinBytes);
} catch (Exception e) {
Log.e(TAG, "Error occurs when trying to auto pair");
e.printStackTrace();
}
}
}
};
/* 连接设备后我正在创建债券*/
@Override
public void onDeviceConnected(BluetoothDevice device) {
device.createBond();
}
您可以绕过本机蓝牙配对过程并以编程方式与蓝牙外围设备配对。试试这个:
为 BluetoothDevice.ACTION_PAIRING_REQUEST
注册具有最高优先级的接收器。
private void notPaired(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
filter.setPriority(SYSTEM_HIGH_PRIORITY-1);
registerReceiver(mReceiver, filter);
mDevice.createBound();// OR establish connection with the device and read characteristic for triggering the pairing process
getBoundState();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
if(type == BluetoothDevice.PAIRING_VARIANT_PIN){
byte[] pin = "123456".getBytes();
device.setPin(pin);
Log.i("Pairing Process ", "Pairing Key Entered");
abortBroadcast();
}else
Log.i("Pairing Process: ", "Unexected Pairing type");
}
}
};
为确保设备已配对,请为 BluetoothDevice.ACTION_BOND_STATE_CHANGED
private void getBoundState(){
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(boundStateReciver, filter);
}
private final BroadcastReceiver boundStateReciver= new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
final int d = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,-1);
switch(d){
case BluetoothDevice.BOND_BONDED:
Log.i("Pairing Process ", "Paired successfully");
break;
}
}
}
};
在清单中添加 this 权限
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />