Unsecured Bluetooth connection in Android

久未见 提交于 2019-12-21 05:03:35

问题


I have been challenged by a professor to develop a little Bluetooth Demo app on Android. I knew nothing about developping for Android until 2 weeks ago when he gave me that challenge. I'm also quite new at Java programming in general, so I'm starting from far. But anyway...

So I did most of the tutorial, and I read about Bluetooth in Android, looked at the Bluetooth Chat sample code, and I'm now trying to do my little app. So for my demo, I will try to establish a connection between my real phone and my Bluetooth mouse. I want to move a shape on the screen of my phone in response to my mouse movement.

I encounter many problem, but so far my main one is to open a socket with my unsecure mouse. When I try using the method listenUsingRfcommWithServiceRecord, it ask a UUID as a parameter. But my mouse most likely doesn't have a UUID to respond, so I guess this method is not the good one.

When I read the documentation about this method, it says that to open an unsecure server socket with a device like a mouse, I must use the listenUsingInsecureRfcommWithServiceRecord method. But this method is not available when I call it, it gets underlined in red and Eclipse says that it is undefined for the type BluetoothAdapter.

private BluetoothServerSocket connectDevice(BluetoothAdapter adapter, BluetoothDevice device){
    BluetoothServerSocket socket = null;
    try{
        socket = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(), UUID.randomUUID());
    }
    catch(IOException e){
        Toast.makeText(this, "Connection failed.\n" + e.getMessage(), Toast.LENGTH_SHORT);
    }

    return socket;
}

Please don't flame me if I'm doing it all wrong, it's my first question here and I'm starting with Java programming.


回答1:


listenUsingInsecureRfcommWithServiceRecord()

This is only available on API Level 10 and later, i.e., Android v2.3.3 onwards.

That may be the problem if you're building for a version previous to that.

See to the right-hand side of the grey bar in the docs

EDIT: In light of the fact it isn't possible to extend BluetoothAdapter, listenUsingInsecureRfcommWithServiceRecord() simply does this...

return createNewRfcommSocketAndRecord(name, uuid, false, false);

The source for createNewRfcommSocketAndRecord() (which is a private method of BluetoothAdapter), can be found here...createNewRfcommSocketAndRecord

Not sure if will help but you might be able to reproduce its functionality.




回答2:


If you are trying to talk to a commercial mouse - then using the SPP socket APIs in android will not help, The mice uses the HID Bluetooth profile , and it requires the phone to have the HID profile host role available. Standard android release don't support HID currently - so you will have to add it yourself and build android integrating HID from BlueZ and connecting it to your application.




回答3:


For Bluetooth profile support to be implemented on Android, there is a project called “Sybase-iAnywhere-Blue-SDK-for-Android”, which replaces Android's version, and provides all interfaces into the underlying Bluetooth profiles and protocols. Using this, printing over bluetooth using your Android phone will be possible using the BPP profile provided by this SDK.

See links below for more details: link 1: http://www.sybase.com/detail?id=1064424

Link 2: http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks



来源:https://stackoverflow.com/questions/5607173/unsecured-bluetooth-connection-in-android

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