一. 参考
-
Bluetooth overview
https://developer.android.com/guide/topics/connectivity/bluetooth -
Android BluetoothChat Sample
https://github.com/googlearchive/android-BluetoothChat
二. 简单概述
- Find devices (发现设备)
- Connect devices(连接设备)
- Manage a connection(进行数据传输)
三. 分步骤介绍
(一) Find devices (发现设备)
1.Query paired devices(查询已配对设备)
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
2 . Discover devices (发现设备)
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
// Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
(二). Connect devices(连接设备)
1 . Connect as a client (连接作为一个客户端发起者)
// Get a BluetoothSocket to connect with the given BluetoothDevice.
// MY_UUID is the app's UUID string, also used in the server code.
BluetoothSocket mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
// Connect to the remote device through the socket. This call blocks
// until it succeeds or throws an exception.
mmSocket.connect();
2 . Connect as a server(连接作为一个服务端监听连接)
// MY_UUID is the app's UUID string, also used by the client code.
BluetoothServerSocket mmServerSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
BluetoothSocket socket = mmServerSocket.accept();
(三).Manage a connection(进行数据传输)
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
mState = STATE_CONNECTED;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (mState == STATE_CONNECTED) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
*
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}