最近在做一个蓝牙的项目,实现手机和蓝牙的通信。
在这里把关键的步骤总结一下,当作回顾和分享了,觉得有用拿去用吧,嘻嘻
开发的步骤:
1,打开蓝牙
2,发现蓝牙
3,连接蓝牙
4,实现数据通信
下面就按照这几个步骤去写
首先获取蓝牙的适配器
BluetoothAdapter defaultAdapter=BluetoothAdapter.getDefaultAdapter();
拿到适配器以后我们需要判断是否为空,因为有可能手机不支持蓝牙
判断蓝牙是否打开,没有打开我们就打开
打开方式有2种
1,发送代码打开
if (defaultAdapter!= null && !defaultAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent)
}
2,后台自动打开
if (defaultAdapter!= null && !defaultAdapter.isEnabled()) {
defaultAdapter.enable();
}
发现蓝牙设备
开始扫描设备
defaultAdapter.startDiscovery();
蓝牙的状态是通过广播的形式给开发者的
所以我们注册广播接收站,获取蓝牙的状态
receiver = new BTReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//开始扫描
filter.addAction(BluetoothDevice.ACTION_FOUND);//发现设备
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//结束扫描
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //绑定状态改变
filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);//连接状态改变
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//蓝牙开关状态改变
registerReceiver(receiver, filter);
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case BluetoothDevice.ACTION_FOUND:
//获取扫描到的设备
BluetoothDevice device = intent.
getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//在这里获取扫描到的设备,然后我们可以去连接设备
//可能有很多的设备,我们可以去让用户选择,点击连接,这里
//就用以LinearLayout去展示一下
TextView textView =
new TextView(MainActivity.this);
textView.setText("名字:"+device.getName() +
"地址" + device.getAddress()+
"绑定状态:"+device.getBondState()+
"蓝牙的类型:"+device.getType()+"uuid");
ll_text.addView(textView);
textView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
//绑定(匹配设备)
device.createBond();
//根据UUID获取设备的输入输出流
try {
BluetoothSocket socket =
device.createRfcommSocketToServiceRecord(
UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
//连接设备
socket.connect();
System.out.println("连接成功,开始获取输出流");
outputStream = socket.getOutputStream();//获取输出流就可以发送数据到设备了
//获取输入流读设备发送给手机的信息
InputStream inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
//开始扫描
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
//结束扫描
break;
case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
//绑定状态改变
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
//蓝牙开关状态改变
int state = intent.getIntExtra(
BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_ON:
break;
}
break;
}
}
}
/**
这种连接方式没有用过,在网上看到的,也贴上去把,听说是连接手机或者蓝牙音响的
*/
if (device.getBluetoothClass().getMajorDeviceClass() != BluetoothClass.Device.Major.AUDIO_VIDEO) {
return;
}
btdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
BluetoothHeadset bluetoothHeadset = (BluetoothHeadset) proxy;
Class btHeadsetCls = BluetoothHeadset.class;
try {
Method connect = btHeadsetCls.getMethod("connect", BluetoothDevice.class);
connect.setAccessible(true);
connect.invoke(bluetoothHeadset, device);
} catch (Exception e) {
Log.e(TAG, e + "");
}
}
@Override
public void onServiceDisconnected(int profile) {
}
}, BluetoothProfile.HEADSET);
到此博客写完了,刚开始写这个东西,还不太熟悉,凑合着看咯,哈哈