[英]Android : Programmatically pair a bluetooth device; Incorrect pin or passkey on some devices?
[英]Defining Passkey for Android Bluetooth Connection
再次需要您的幫助:
我想與obd2-bluetooth-adapter建立連接。 因此,我看了AndroidSDK中的BluetoothChat-Example。 我能夠與計算機建立連接,但無法將android平板電腦與odb2-bluetooth-adapter(elm327)配對。 找到了一些提示,例如:
myRemoteBluetoothDevice.setPassKey(....);
首先,我無法在“ myRemoteBluetoothDevice”上使用該功能-然后我不知道在哪里使用此功能。 在連接線程內?
public synchronized void connect(BluetoothDevice device, boolean secure) {
if (D) Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device, secure);
mConnectThread.start();
setState(STATE_CONNECTING);
}
我認為可能的解決方案是實現事件偵聽器或類似的方法,當遠程設備需要密碼時調用該方法? 但是我必須在哪里實施呢? 我必須在哪里使用它? 有人可以幫我嗎?
提前致謝 !!!
PS:“我的應用”基於以下示例: https : //android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.java
試圖實施第一個答案:
我的BroadcastReceiver:
private final BroadcastReceiver mReceiverRequiresPin = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
try {
BluetoothDevice newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Class<?> btDeviceInstance = Class.forName(BluetoothDevice.class.getCanonicalName());
Method convert = btDeviceInstance.getMethod("convertPinToBytes", String.class);
byte[] pin = (byte[]) convert.invoke(newDevice, "1234");
Method setPin = btDeviceInstance.getMethod("setPin", byte[].class);
boolean success = (Boolean) setPin.invoke(newDevice, pin);
}
catch (Exception e) {
e.printStackTrace();
}
}
};
還有我的連接方法,我在其中注冊broadcastReceiver:
private void connect(CordovaArgs args, boolean secure,
CallbackContext callbackContext) throws JSONException {
final String actionPinRequested = "android.bluetooth.device.action.PAIRING_REQUEST";
IntentFilter intentFilterPinRequested = new IntentFilter(actionPinRequested);
cordova.getActivity().registerReceiver(mReceiverRequiresPin, intentFilterPinRequested);
String macAddress = args.getString(0);
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);
if (device != null) {
connectCallback = callbackContext;
bluetoothSerialService.connect(device, secure);
PluginResult result = new PluginResult(
PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
} else {
callbackContext.error("Could not connect to " + macAddress);
}
}
非常感謝您的幫助! 提前致謝。
沒有人有暗示嗎?
注冊以下廣播偵聽器:android.bluetooth.device.action.PAIRING_REQUEST。
在一次:
BluetoothDevice newDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Class<?> btDeviceInstance = Class.forName(BluetoothDevice.class.getCanonicalName());
Method convert = btDeviceInstance.getMethod("convertPinToBytes", String.class);
byte[] pin = (byte[]) convert.invoke(newDevice, "1234");
Method setPin = btDeviceInstance.getMethod("setPin", byte[].class);
success = (Boolean) setPin.invoke(newDevice, pin);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.