android蓝牙开发的基础知识
1.最近研究蓝牙开发 跟大家分享一下 小白的学习心得
首先了解蓝牙开发中的利器
private BluetoothAdapter mBluetoothAdapter;
它掌控了蓝牙模块的大多功能实现
(1)设备是否有蓝牙模块
//实例化蓝牙适配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//判断蓝牙功能呢是否存在
if (mBluetoothAdapter == null) {
showToast("无蓝牙模块");
return;
}
(2)获取蓝牙信息,以及运行状态
//获取名字和mac地址/当前蓝牙状态(打开,关闭,正在连接.....)
String name = mBluetoothAdapter.getName();
String mac = mBluetoothAdapter.getAddress();
int state = mBluetoothAdapter.getState();
//判断状态
switch (state) {
case BluetoothAdapter.STATE_ON: //蓝牙已经打开
break;
case BluetoothAdapter.STATE_TURNING_ON://蓝牙正在打开
break;
case BluetoothAdapter.STATE_TURNING_OFF://蓝牙正在关闭
break;
case BluetoothAdapter.STATE_OFF://蓝牙已经关闭
break;
}
(3)两种打开蓝牙方式
public static final int REQUEST_OPEN = 0x01;//打开一个蓝牙
//强制打开蓝牙
/*boolean isOpen = mBluetoothAdapter.enable();
showToast("" + isOpen);*/
//调用系统API中action打开(start Activity for result)
Intent open = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(open, REQUEST_OPEN);
(4)想看源码的下载 下demo (免费滴)
http://download.csdn.net/download/gywuhengy/9813413