Connect to specific Bluetooth device with a click

落爺英雄遲暮 提交于 2019-12-04 19:45:54
Yvette Colomb

It's impossible to give you an example within this format, so I have provided you with a good sample and helpful links to help you understand the sample.

I recommend you follow the steps I have provided and then, when you have specific problems, you can bring it here, with the code snippet you are having difficulty with.

I recommend you use download this sample code:
http://developer.android.com/samples/BluetoothChat/index.html

If you haven't already, it's good to study this:
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

This is a good tutorial and they have many tutorials:
http://www.tutorialspoint.com/android/android_bluetooth.htm

You will need the following permissions in your manifest:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

This is one intent that is advisable to use, to check to see if BT is enabled:

if (!mBluetoothAdapter.isEnabled()) {
    android.content.Intent enableIntent = new android.content.Intent(
           android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}

and to make your device discoverable to other devices:

if (mBluetoothAdapter.getScanMode() !=
        android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
    android.content.Intent discoverableIntent =
        new android.content.Intent(
            android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(
        android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                300); // You are able to set how long it is discoverable.
    startActivity(discoverableIntent);
}

As I mentioned in my answer here:

You can avoid using an intent to search for paired devices. When connecting to a device that is not paired, a notification will pop up asking to pair the devices. Once paired this message should not show again for these devices, the connection should be automatic (according to how you have written your program).

I use an intent to enable bluetooth, and to make my device discoverable, I then set up my code to connect, and press a button to connect. In your case, you will need to ensure your accessories are discoverable also. In my case I use a unique UUID, and both devices must recognise this to connect. This can only be used if you are programming both devices, whether both are android or one android and one other device type.

You will need to understand how to use sockets, this is how the devices communicate.
I recommend studying these two links:

http://developer.android.com/reference/android/bluetooth/BluetoothSocket.html
http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html

Sockets are used to set up connections between devices. There will be a server socket and device sockets (determined by many factors, the programmer, the actual devices). The server socket listens for incoming connections, when a connection is accepted the devices connect, each with a simple socket.

I am not sure how much you know about threading. The connection needs to be managed with threads:

http://developer.android.com/guide/components/processes-and-threads.html
http://android-developers.blogspot.com.au/2009/05/painless-threading.html

The connection between the devices is managed on threads separate from the User Interface thread. This is to prevent the phone from locking up while it is setting up, seeking and making a BT connection.

For instance:

AcceptThread - the thread that listens for a connection and accepts the connection (via the serversocket). This thread can run for an extended time waiting for a device to connect with.

ConnectThread - the thread that the device connecting to the server uses to connect to the serversocket.

ConnectedThread - this is the thread that manages the connection between both sockets.

Let me know if this helps you.

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