Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

sockets - Android: getBluetoothService() called with no BluetoothManagerCallback

I'm trying to connect my Nexus 4 with a Wii Balance Board but I get this error:

getBluetoothService() called with no BluetoothManagerCallback
connect(), SocketState: INIT, mPfd: null

So it doesn't finish the connection.

My socket:

public final class wSocket
{
    public static BluetoothSocket create(BluetoothDevice dev, int port)
    {
        try {
        /*
         * BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, BluetoothDevice device, int port, ParcelUuid uuid)
         */
            Constructor<BluetoothSocket> construct = BluetoothSocket.class.getDeclaredConstructor(int.class, int.class, boolean.class,
                boolean.class, BluetoothDevice.class, int.class, ParcelUuid.class);

            construct.setAccessible(true);
            return construct.newInstance(3 /* TYPE_L2CAP */, -1, false, false, dev, port, null);
        } catch (Exception ex) {
            return null;
        }
    }
}

Where it gives me the error:

private BluetoothSocket sk;
...
sk = wSocket.create(wm.dev, 0x11);
...
sk.connect();

I have checked this link with no success because I just open 1 socket: getbluetoothservice() called with no bluetoothmanagercallback

Any help or idea to explore?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to get BluetoothAdapter via getDefaultAdapter() prior to socket object create. It seems that callback service is created when reference to BLuetoothAdater is taken by above mentioned call. For details : https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothAdapter.java

where mService = managerService.registerAdapter(mManagerCallback); is loaded with value when getDefaultAdapter is called.

for socket connect() the getBluetoothService() argument is always null, see code below:

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothSocket.java

p.s. it seems that google does not advertise usage of BluetoothSocket constructor directly and asking to use method of BluetoothDevice to get socket created.(from reference on google site) the reason behind is not known to me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...