Android Bluetooth Connection Secure Insecure

杀马特。学长 韩版系。学妹 提交于 2019-12-20 16:52:25

问题


I have been playing around with the bluetooth API for Android 2.2 (API level 8, HTC Desire) and had an app connecting to an embedded Bluetooth device using:

device.createRfcommSocketToServiceRecord(DEV_UUID);

This generated a pairing request as expected, however to streamline the connection process I wanted to avoid the user interaction when pairing so moved to API level 10 (HTC Desire with CyanogenMod 7) so I could use:

 device.createInsecureRfcommSocketToServiceRecord(DEV_UUID);

When testing this also works as expected (connecting without prompting the user to pair), however when I try to create the secure RfcommSocket under API level 10 as before with 2.2 I get a connection refused exception...

 java.io.IOException: Connection refused
    at android.bluetooth.BluetoothSocket.connectNative(Native Method)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)

As far as I can tell this should still work in the same way, prompting the user to pair?

EDIT:

Just tried again using the following code and the outcome is the same (working for insecure but not for secure), I will try and get my hands on a stock 2.3 device to test on.

        try {
            Method m = dev.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class } );
            BluetoothSocket bs = (BluetoothSocket)m.invoke(dev, devUUID);
            Log.d("TEST", "Method Invoked");
            bs.connect();
            Log.d("TEST", "Connected to socket");
            bs.close();
            Log.d("TEST", "Closed Socket");
        }

回答1:


While looking for the solution of similar problem in my app, I have found this blog from code.google.com

It will help all those who are still looking for this problem solution on SO

http://mobisocial.stanford.edu/news/2011/03/bluetooth-reflection-and-legacy-nfc/ (link not working anymore)

The solution has become very simple now. Just include InsecureBluetooth.java in your project and change 2 lines in BluetoothChatService.java.

tmp = InsecureBluetooth.listenUsingRfcommWithServiceRecord(mAdapter, NAME, MY_UUID, true);

and

tmp   = InsecureBluetooth.createRfcommSocketToServiceRecord(device, MY_UUID, true);

Thats it !



来源:https://stackoverflow.com/questions/8282481/android-bluetooth-connection-secure-insecure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!