android 蓝牙2.0 demo,Android蓝牙2.0开发

因项目需要,做一个与2.0蓝牙模块连接的Demo,在此记录一下

1.先注册个广播,来接收一下蓝牙的状态

MyBroadCastRevciver broadcastReceiver = null;

public void registerBroadcast(){

broadcastReceiver = new MyBroadCastRevciver();

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(BluetoothDevice.ACTION_FOUND);

intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);

intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

registerReceiver(broadcastReceiver, intentFilter);

}

private String mac;

class MyBroadCastRevciver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

switch (intent.getAction()) {

case BluetoothDevice.ACTION_FOUND:

Log.d("TAG", "找到蓝牙");

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

mac = device.getAddress();

break;

case BluetoothDevice.ACTION_BOND_STATE_CHANGED:

device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

switch (device.getBondState()) {

case BluetoothDevice.BOND_NONE:

Log.d("TAG", "取消配对");

break;

case BluetoothDevice.BOND_BONDING:

Log.d("TAG", "配对中");

break;

case BluetoothDevice.BOND_BONDED:

Log.d("TAG", "配对成功");

break;

}

break;

case BluetoothDevice.ACTION_ACL_CONNECTED:

Log.d("TAG", "连接");

break;

case BluetoothDevice.ACTION_ACL_DISCONNECTED:

Log.d("TAG", "断开");

break;

default:

break;

}

}

}

2.获取BluetoothAdapter并初始化蓝牙,此类是操作本地蓝牙的适配器

BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();

if (defaultAdapter == null ){

Log.d("TAG","此设备不支持蓝牙");

}

if(!defaultAdapter.isEnabled()){

//蓝牙未打开,开启蓝牙

activity.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),1);

}

3.搜索蓝牙

/**

* 搜索蓝牙

*/

public void searchBlueTooth() {

if (defaultAdapter != null) {

Log.d("TAG", "搜索蓝牙");

if (!defaultAdapter.isDiscovering()) {

defaultAdapter.startDiscovery();

}

}

}

4.找到目标蓝牙后需要先配个对

private void bondDevice(){

BluetoothDevice device = null;

if (defaultAdapter != null) {

device = defaultAdapter.getRemoteDevice(mac);

}

if (device.getBondState() == BluetoothDevice.BOND_BONDED) {

Log.d("TAG", "已配对");

} else {

Log.d("TAG", "配对");

Method method = BluetoothDevice.class.getMethod("createBond");

method.invoke(device);

}

}

5.配对后就可以连接了

class BlueToothThread extends Thread {

BluetoothDevice device;

BluetoothSocket socket;

public BlueToothThread(BluetoothDevice device) {

this.device = device;

}

@Override

public void run() {

try {

Log.d("TAG", "连接蓝牙");

socket = device.createRfcommSocketToServiceRecord(ECG_UUID);

//在连接过程中,会出现java.io.IOException: read failed, socket might closed or timeout, read ret: -1,目前还没什么找到太好的解决方案

socket.connect();

InputStream inputStream = null;

OutputStream outputStream = null;

if (socket != null) {

Log.d("TAG", "获取输入流");

inputStream = socket.getInputStream();

outputStream = socket.getOutputStream();

}

byte[] bytes = new byte[100];

if (inputStream != null) {

while (true) {

int read = inputStream.read(bytes);

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

你可能感兴趣的:(android,蓝牙2.0,demo)