简体   繁体   中英

Unsecured Bluetooth connection in Android

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.

listenUsingInsecureRfcommWithServiceRecord()

This is only available on API Level 10 and later, ie, 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM