Mic*_*ter 5 android bluetooth android-bluetooth
连接蓝牙设备的正常方式是通过配对。
\n\n我们需要以非正常方式连接到设备:仅使用蓝牙 MAC 地址。我们不希望系统提示输入 PIN。
\n\n我们知道该设备支持此技术,但我们找不到在 Android 上执行此操作的方法。
\n\n缩写代码如下所示:
\n\n String mac_address = \xe2\x80\x9c00:11:22:33:44:55\xe2\x80\x9d\n BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();\n\n BluetoothDevice bluetoothDevice = mBluetoothAdapter.getRemoteDevice(mac_address);\n\n Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", \n new Class[] {int.class});\n BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);\n\n socket.connect();\n
Run Code Online (Sandbox Code Playgroud)\n\n问题是,系统socket.connect()
会提示我们输入 PIN 码。此设备不需要 PIN,因此我们不希望系统提示输入 PIN。
我们不需要知道 PIN 码的原因是:
\n\n我们如何修改代码,使其不会提示输入 PIN 码?
\n\n[编辑测试评论中的建议]
\n\n我们测试了 createInsecureRfcommSocketToServiceRecord 方法,如下所示:
\n\nParcelUuid list[] = device.getUuids();\nBluetoothSocket socket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(list[0].getUuid());\n
Run Code Online (Sandbox Code Playgroud)\n\n然后我们得到这样的错误:
\n\nI/InputDispatcher( 382): Delivering touch to current input target: action: 0x1\nI/InputDispatcher( 382): Delivering touch to current input target: action: 0x1\nV/BluetoothSocket.cpp(16956): initSocketNative\nV/BluetoothSocket.cpp(16956): ...fd 66 created (RFCOMM, lm = 0)\nV/BluetoothSocket.cpp(16956): initSocketFromFdNative\nI/BluetoothPolicyService( 382): getBluetoothDataTransferAllowed\nD/BluetoothUtils(16956): isSocketAllowedBySecurityPolicy start : device null\nV/BluetoothService.cpp( 382): createDeviceNative\nV/BluetoothService.cpp( 382): ... address =\nV/BluetoothEventLoop.cpp( 382): onCreateDeviceResult\nV/BluetoothEventLoop.cpp( 382): onCreateDeviceResult\nE/BluetoothEventLoop.cpp( 382): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)\nD/BluetoothEventLoop( 382): Result of onCreateDeviceResult:1\nV/BluetoothService.cpp( 382): discoverServicesNative\nV/BluetoothService.cpp( 382): ... Object Path = /org/bluez/1100/hci0/dev_00_11_22_33_44_55\nV/BluetoothService.cpp( 382): ... Pattern = , strlen = 0\nD/FStest ( 633): check default\nD/FStest ( 633): check default\n
Run Code Online (Sandbox Code Playgroud)\n
小智 2
这绝对是可能的。您应该查看 Android BluetoothChat 演示应用程序: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;
}
Run Code Online (Sandbox Code Playgroud)
在客户端。在服务器端:
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;
}
Run Code Online (Sandbox Code Playgroud)
请记住,为此您需要蓝牙 MAC 地址。谷歌正试图隐藏该地址并使其无法使用(以防止跟踪)。您在获取自己的 MAC 地址时会遇到问题,并且远程设备的 MAC 地址是随机的,并且在一段时间后会发生变化:http ://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior -硬件 ID
归档时间: |
|
查看次数: |
13032 次 |
最近记录: |