从适配器收听时设置蓝牙身份验证 PIN
set bluetooth authentication PIN when listening from adapter
我正在制作一个需要连接蓝牙设备并从中获取数据的应用程序...该设备被设置为主设备,所以我需要实现 Thread
,其中我 listenUsingRfcommWithServiceRecord
并等待来自它的连接:
public AcceptThread(Context context, String serverName, UUID myUUID) {
this.context = context;
BluetoothServerSocket tmp = null;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serverName, myUUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
然后 run
我 运行 代码 socket = mmServerSocket.accept(5000)
等待它开始与设备配对:
public void run() {
BluetoothSocket socket = null;
while (true) {
try {
socket = mmServerSocket.accept(5000);
} catch (IOException e) {
Log.e(TAG,"IOException: " + e);
}
// If a connection was accepted
if (socket != null) {
// Manage the connection
ManageConnectedSocket manageConnectedSocket = new ManageConnectedSocket(socket);
manageConnectedSocket.run();
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "IOException: " + e);
}
break;
}
}
}
该设备要求提供身份验证 PIN,我需要一个自动程序...为此,我虽然实现了一个广播接收器以了解该设备何时被要求与另一台设备进行比较:
IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
context.registerReceiver(mPairReceiver, filter);
并收到它:
private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
Log.e(TAG,"ACTION_PAIRING_REQUEST");
setBluetoothPairingPin(device);
}
}
};
在我的 setBluetoothPairingPin
方法中,我收到一个 BluetoothDevice
对象:
public void setBluetoothPairingPin(BluetoothDevice device) {
byte[] pinBytes = convertPinToBytes("0000");
try {
Log.e(TAG, "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
Log.e(TAG, "Success to add the PIN.");
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, false);
Log.e(TAG, "Success to setPairingConfirmation.");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
问题是我无法知道我的套接字何时收到信息,并且在连接之前无法知道我的 BluetoothDevice
设置 Pairing Pin 是什么...
有人可以帮助我如何超越这个吗?或者当我从 BluetoothServerSocket
收听时,还有其他方法可以进行 PIN 验证吗?
如果我没有正确解释,请告诉我...
提前致谢
在 this and this 的帮助下,我能够为我工作...
我对方法 setBluetoothPairingPin
感到困惑,我无法理解 ACTION_PAIRING_REQUEST
实际上是在设备开始配对时调用的,也就是在从中询问 PIN 时用户...所以调用 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
,并更改一些我设法使其工作的设置配对方法...
这是我的最终代码:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String PIN = "0000";
byte[] pin = new byte[0];
try {
pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, PIN);
BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
我正在制作一个需要连接蓝牙设备并从中获取数据的应用程序...该设备被设置为主设备,所以我需要实现 Thread
,其中我 listenUsingRfcommWithServiceRecord
并等待来自它的连接:
public AcceptThread(Context context, String serverName, UUID myUUID) {
this.context = context;
BluetoothServerSocket tmp = null;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serverName, myUUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
然后 run
我 运行 代码 socket = mmServerSocket.accept(5000)
等待它开始与设备配对:
public void run() {
BluetoothSocket socket = null;
while (true) {
try {
socket = mmServerSocket.accept(5000);
} catch (IOException e) {
Log.e(TAG,"IOException: " + e);
}
// If a connection was accepted
if (socket != null) {
// Manage the connection
ManageConnectedSocket manageConnectedSocket = new ManageConnectedSocket(socket);
manageConnectedSocket.run();
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "IOException: " + e);
}
break;
}
}
}
该设备要求提供身份验证 PIN,我需要一个自动程序...为此,我虽然实现了一个广播接收器以了解该设备何时被要求与另一台设备进行比较:
IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
context.registerReceiver(mPairReceiver, filter);
并收到它:
private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
Log.e(TAG,"ACTION_PAIRING_REQUEST");
setBluetoothPairingPin(device);
}
}
};
在我的 setBluetoothPairingPin
方法中,我收到一个 BluetoothDevice
对象:
public void setBluetoothPairingPin(BluetoothDevice device) {
byte[] pinBytes = convertPinToBytes("0000");
try {
Log.e(TAG, "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
Log.e(TAG, "Success to add the PIN.");
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, false);
Log.e(TAG, "Success to setPairingConfirmation.");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
问题是我无法知道我的套接字何时收到信息,并且在连接之前无法知道我的 BluetoothDevice
设置 Pairing Pin 是什么...
有人可以帮助我如何超越这个吗?或者当我从 BluetoothServerSocket
收听时,还有其他方法可以进行 PIN 验证吗?
如果我没有正确解释,请告诉我...
提前致谢
在 this and this 的帮助下,我能够为我工作...
我对方法 setBluetoothPairingPin
感到困惑,我无法理解 ACTION_PAIRING_REQUEST
实际上是在设备开始配对时调用的,也就是在从中询问 PIN 时用户...所以调用 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
,并更改一些我设法使其工作的设置配对方法...
这是我的最终代码:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String PIN = "0000";
byte[] pin = new byte[0];
try {
pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, PIN);
BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}