2010年12月2日星期四

Android Bluetooth

Android Bluetooth

Bluetooth is a communication protocol designed for short-range, low-bandwidth, peer-to-peer communications.
Bluetooth devices and connections are only managed by the following classes:
l         BluetoothAdapter local Bluetooth adapter
l         BluetoothDevice remote Bluetooth device
l         BluetoothSocket
l         BluetoothServerSocket

Accessing the local BT adapter

BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter();
In order to access the local BT adapter, you should include the Bluetooth manifest permission.
"android.permission.BLUETOOTH" />
"android.permission.BLUETOOTH_ADMIN" />

Managing BT Properties and State

The most useful properties are friendly name and MAC address:
mBTAdapter.getName()
mBTAdapter.getAddress()
The current BT Adpater’s state:
l         STATE_TURNING_ON
l         STATE_ON
l         STATE_TURNING_OFF
l         STATE_OFF

Turning on local BT Adapter

private static final int RQUEST_ENABLE_BT = 0;
Intent enalbeIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, RQUEST_ENABLE_BT);
A sub-activity will be pop-up. The result code of the sub-activity will be returned on the callback method onActivityResult(int requestCode, int resultCode, Intent data).

Being discoverable

The BT Discoverability is indicated by the scan mode.
There are three scan modes (getScanMode()).
SCAN_MODE_CONNECTABLE_DISCOVERABLE
SCAN_MODE_CONNECTABLE
SCAN_MODE_NONE
To turn on discovery you need to obtain permission from the user by starting an Activity:
String dis = BluetoothAdapter.ACTION_RQUEST_DISCOVERABLE;
Intent mDis = new Intent(dis);
StartActivityForResult(mDis, DISCOVERY_REQUEST);
To start discovery process, you should call function
mAdapter.startDiscovery();
To cancel discovery process, you should call function
mAdapter.cancelDiscovery();
To get the remote bluetooth device discovered, you should use the BroadcastReceiver,
registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));
BroadcastReceiver discoveryResult = new BroadcastReceiver(){
public void onReceiver(Context ct, Intent intent){
   String mRemoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
   BluetoothDevice mRemoteDevice;
   mRemoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
}
}

Bluetooth Communications

The communication channel is a RFCOMM channel.
To establish the bidirectional communications, you should use the following classes:
l         BluetoothServerSocket  A bluetooth server socket is used to listen for incoming Bluetooth Socket connection request from remote Bluetooth Devices.
l         BluetoothSocket
The server socket is created by listenUsingRfcommWithServiceRecord method on mAdapter. The server socket is identified by a name and a UUID.
To start listening for connections call accept on the server.

Transmitting Data Using Bluetooth Sockets

Once a channel is established, the two peers can talk with each other by BluetoothSocket.

没有评论: