连接蓝牙设备的正常方式是通过配对。
我们需要以非正常方式连接到设备:仅使用蓝牙 MAC 地址。我们不希望系统提示输入 PIN。
我们知道该设备支持此技术,但我们找不到在 Android 上执行此操作的方法。
缩写代码如下所示:
String mac_address = “00:11:22:33:44:55”
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(mac_address);
Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket",
new Class[] {int.class});
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
问题是当socket.connect()
系统会提示我们输入 PIN 码。此设备不需要 PIN,因此我们不希望系统提示输入 PIN。
我们不需要知道 PIN 码的原因是:
- 我们制造该设备并编写固件。
- 我们可以使用 Windows 上用 C# 编写的程序连接到它。
我们如何修改代码,使其不会提示输入 PIN 码?
[编辑测试评论中的建议]
我们测试了 createInsecureRfcommSocketToServiceRecord 方法,如下所示:
ParcelUuid list[] = device.getUuids();
BluetoothSocket socket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(list[0].getUuid());
然后我们得到这样的错误:
I/InputDispatcher( 382): Delivering touch to current input target: action: 0x1
I/InputDispatcher( 382): Delivering touch to current input target: action: 0x1
V/BluetoothSocket.cpp(16956): initSocketNative
V/BluetoothSocket.cpp(16956): ...fd 66 created (RFCOMM, lm = 0)
V/BluetoothSocket.cpp(16956): initSocketFromFdNative
I/BluetoothPolicyService( 382): getBluetoothDataTransferAllowed
D/BluetoothUtils(16956): isSocketAllowedBySecurityPolicy start : device null
V/BluetoothService.cpp( 382): createDeviceNative
V/BluetoothService.cpp( 382): ... address =
V/BluetoothEventLoop.cpp( 382): onCreateDeviceResult
V/BluetoothEventLoop.cpp( 382): onCreateDeviceResult
E/BluetoothEventLoop.cpp( 382): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
D/BluetoothEventLoop( 382): Result of onCreateDeviceResult:1
V/BluetoothService.cpp( 382): discoverServicesNative
V/BluetoothService.cpp( 382): ... Object Path = /org/bluez/1100/hci0/dev_00_11_22_33_44_55
V/BluetoothService.cpp( 382): ... Pattern = , strlen = 0
D/FStest ( 633): check default
D/FStest ( 633): check default
这绝对是可能的。您应该查看 Android BluetoothChat 演示应用程序:http://developer.android.com/samples/BluetoothChat/src/com.example.android.bluetoothchat/BluetoothChatService.html http://developer.android.com/samples/BluetoothChat/src/com.example.android.bluetoothchat/BluetoothChatService.html他们使用不安全的渠道:
public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(
MY_UUID_SECURE);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(
MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
mmSocket = tmp;
}
在客户端。在服务器端:
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
// Create a new listening server socket
try {
if (secure) {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
MY_UUID_SECURE);
} else {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
NAME_INSECURE, MY_UUID_INSECURE);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
}
mmServerSocket = tmp;
}
请记住,为此您需要蓝牙 MAC 地址。谷歌正试图隐藏该地址并使其无法使用(以防止跟踪)。您在获取自己的 MAC 地址时会遇到问题,并且远程设备的 MAC 地址是随机的,并且在一段时间后会发生变化:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)