蓝牙,是一种支持设备短距离通信(一般10m内)的无线电技术。能在包括移动电话、PDA、无线耳机、笔记本电脑、相关外设等众多设备之间进行无线信息交换。利用“蓝牙”技术,能够有效地简化移动通信终端设备之间的通信,也能够成功地简化设备与因特网Internet之间的通信,从而数据传输变得更加迅速高效,为无线通信拓宽道路。蓝牙采用分散式网络结构以及快跳频和短包技术,支持点对点及点对多点通信,工作在全球通用的2.4GHz ISM(即工业、科学、医学)频段。其数据速率为1Mbps。采用时分双工传输方案实现全双工传输。
蓝牙权限
蓝牙权限不属于危险权限,所以写在清单文件即可,Android6.0不需要动态申请。但是搜索周边蓝牙设备是需要定位权限的,而且要求gps打开。
允许程序连接到已配对的蓝牙设备。
允许程序发现和配对蓝牙设备
初始化蓝牙
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (null == mBluetoothAdapter) {
Toast.makeText(this.getApplicationContext(), R.string.not_support_bluetooth, Toast.LENGTH_SHORT).show();
return;
}
开启蓝牙
if(!mBluetoothAdapter.isEnabled()){
Toast.makeText(MainActivity.this, getString(R.string.open_bluetooth), 2000).show();
return;
}
或者
mBluetoothAdapter.enable()
搜索蓝牙
mBluetoothAdapter.startDiscovery();
//TODO:搜索蓝牙之前需要判断定位权限和GPS开关 MIUI权限需要定制处理
if (PermissionUtil.getInstance().checkGPSPermission(MainActivity.this)) {if (!AppUtil.isGpsOPen(MainActivity.this) && PermissionUtil.getInstance().isOverMarshmallow()) {permissionDialog =new PermissionDialog(MainActivity.this, R.style.Dialog); permissionDialog.setOnShowListener(new DialogInterface.OnShowListener() {@Override public void onShow(DialogInterface dialog) {permissionDialog.setTvContent(getResources().getString(R.string.forbid_tip_gps_open)); permissionDialog.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {//跳转GPS设置界面 Intent intent =new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, GPS_OPEN_CODE); } }); } }); permissionDialog.show(); return; }}
停止搜索
mBluetoothAdapter.cancelDiscovery();
//BluetoothAdapter.ACTION_DISCOVERY_FINISHED会回调
注册广播
IntentFilter filter = new IntentFilter();
//发现设备
filter.addAction(BluetoothDevice.ACTION_FOUND);
//设备连接状态改变
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
//蓝牙设备状态改变
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//扫描结束
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHEDD);
registerReceiver(mBluetoothReceiver, filter);
接收广播
//接受蓝牙状态信息并更新界面
private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothDevice.ACTION_FOUND:
// TODO: 2017/11/3 需要请求位置权限才能弹对话框 在这里找到指定public address的蓝牙设备进行连接哦!
BluetoothDevice device1 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtil.e(TAG, "address is " + device1.getAddress() + "\tname is " + device1.getName());
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED: {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
LogUtil.i(TAG, "正在与" + device.getName() + "__" + device.getAddress() + "配对");
break;
case BluetoothDevice.BOND_BONDED:
LogUtil.i(TAG, "与" + device.getName() + "__" + device.getAddress() + "完成配对");
HideDialog();
break;
case BluetoothDevice.BOND_NONE:
LogUtil.i(TAG, "取消与" + device.getName() + "__" + device.getAddress() + "配对");
default:
break;
}
break;
}
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
LogUtil.e(TAG, "扫描完成!!!");
if (!mFound) {
CToast.makeText(MainActivity.this, getString(R.string.please_set_bt_visiable), CToast.LIGHT_WHITE).show();
}
break;
}
}
};
蓝牙权限
蓝牙权限不属于危险权限,所以写在清单文件即可,Android6.0不需要动态申请。但是搜索周边蓝牙设备是需要定位权限的,而且要求gps打开。
允许程序连接到已配对的蓝牙设备。
允许程序发现和配对蓝牙设备
初始化蓝牙
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (null == mBluetoothAdapter) {
Toast.makeText(this.getApplicationContext(), R.string.not_support_bluetooth, Toast.LENGTH_SHORT).show();
return;
}
开启蓝牙
if(!mBluetoothAdapter.isEnabled()){
Toast.makeText(MainActivity.this, getString(R.string.open_bluetooth), 2000).show();
return;
}
或者
mBluetoothAdapter.enable()
搜索蓝牙
mBluetoothAdapter.startDiscovery();
//TODO:搜索蓝牙之前需要判断定位权限和GPS开关 MIUI权限需要定制处理
if (PermissionUtil.getInstance().checkGPSPermission(MainActivity.this)) {if (!AppUtil.isGpsOPen(MainActivity.this) && PermissionUtil.getInstance().isOverMarshmallow()) {permissionDialog =new PermissionDialog(MainActivity.this, R.style.Dialog); permissionDialog.setOnShowListener(new DialogInterface.OnShowListener() {@Override public void onShow(DialogInterface dialog) {permissionDialog.setTvContent(getResources().getString(R.string.forbid_tip_gps_open)); permissionDialog.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {//跳转GPS设置界面 Intent intent =new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, GPS_OPEN_CODE); } }); } }); permissionDialog.show(); return; }}
停止搜索
mBluetoothAdapter.cancelDiscovery();
//BluetoothAdapter.ACTION_DISCOVERY_FINISHED会回调
注册广播
IntentFilter filter = new IntentFilter();
//发现设备
filter.addAction(BluetoothDevice.ACTION_FOUND);
//设备连接状态改变
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
//蓝牙设备状态改变
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//扫描结束
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHEDD);
registerReceiver(mBluetoothReceiver, filter);
接收广播
//接受蓝牙状态信息并更新界面
private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothDevice.ACTION_FOUND:
// TODO: 2017/11/3 需要请求位置权限才能弹对话框 在这里找到指定public address的蓝牙设备进行连接哦!
BluetoothDevice device1 = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtil.e(TAG, "address is " + device1.getAddress() + "\tname is " + device1.getName());
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED: {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
LogUtil.i(TAG, "正在与" + device.getName() + "__" + device.getAddress() + "配对");
break;
case BluetoothDevice.BOND_BONDED:
LogUtil.i(TAG, "与" + device.getName() + "__" + device.getAddress() + "完成配对");
HideDialog();
break;
case BluetoothDevice.BOND_NONE:
LogUtil.i(TAG, "取消与" + device.getName() + "__" + device.getAddress() + "配对");
default:
break;
}
break;
}
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
LogUtil.e(TAG, "扫描完成!!!");
if (!mFound) {
CToast.makeText(MainActivity.this, getString(R.string.please_set_bt_visiable), CToast.LIGHT_WHITE).show();
}
break;
}
}
};