Android蓝牙协议分为两种,SPP协议和BLE(Bluetooth low energy),Android4.3以上加入了低功耗蓝牙即BLE蓝牙协议,本篇文章我主要介绍下spp蓝牙协议的一些特性(BLE协议后期会陆续更新)。
比如我们需要实现一个如下图这样功能
1.如何获取当前手机已经绑定了的蓝牙设备?
// 获取默认的蓝牙adapter
BluetoothAdapter mBtAdapter=BluetoothAdapter.getDefaultAdapter();
// 获取当前可用的蓝牙设置
Set
得到当前的蓝牙设备后,就可用通过遍历pairedDevices ,得到当前手机已经配对过的蓝牙设备。
2.如何扫描得到附近的可用蓝牙设备?
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
mBtAdapter.startDiscovery();
ArrayAdapter
R.layout.device_name);
由于安卓中获取附近蓝牙设备是通过广播机制去接收的,所以得注册广播
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
/**
* 监听搜索到的设备
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
LogUtils.getInstance().e(getClass(), "----ACTION_FOUND---");
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
if(!TextUtils.isEmpty(device.getName())){
if(device.getName().equals("小米手机")){
mNewDevicesAdapter.add(device.getName() + "\n"
+ device.getAddress());
}
}
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
LogUtils.getInstance().e(getClass(), "----ACTION_DISCOVERY_FINISHED----");
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesAdapter.getCount() == 0) {
String noDevices = getResources().getText(
R.string.none_found_device).toString();
mNewDevicesAdapter.add(noDevices);
}
}
}
};
if (device.getBondState() != BluetoothDevice.BOND_BONDED):
如果当前蓝牙设备不是出于已经配对的状态的话,直接添加到可用蓝牙的列表,此处我已经筛选出蓝牙名称必须是“小米手机”
BluetoothDevice.ACTION_FOUND这个action是已经发现的蓝牙事件;
BluetoothAdapter.ACTION_DISCOVERY_FINISHED是蓝牙扫描结束的事件;
整个系列流程的代码我贴在下面:
/**
* @description:此类为扫描和连接蓝牙设备弹出框 扫描和连接蓝牙设备
* @author:zzq
* @time: 2016-8-5 下午午03:59:58
*/
public class DeviceListActivity extends Activity {
public static String EXTRA_DEVICE_ADDRESS = "device_address";
private BluetoothAdapter mBtAdapter;
private ArrayAdapter
private ArrayAdapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.device_list);
setResult(Activity.RESULT_CANCELED);
Button scanButton = (Button) findViewById(R.id.button_scan);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
discoverDevice();
}
});
mPairedDevicesAdapter = new ArrayAdapter
R.layout.device_name);
mNewDevicesAdapter = new ArrayAdapter
R.layout.device_name);
// 已经绑定的设备
ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
pairedListView.setAdapter(mPairedDevicesAdapter);
pairedListView.setOnItemClickListener(mDeviceClickListener);
// 搜索到的可用设备
ListView newListView = (ListView) findViewById(R.id.new_devices);
newListView.setAdapter(mNewDevicesAdapter);
newListView.setOnItemClickListener(mDeviceClickListener);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
// 获取默认的蓝牙adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// 获取当前可用的蓝牙设置
Set
if (pairedDevices.size() > 0) {
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
for (BluetoothDevice device : pairedDevices) {
mPairedDevicesAdapter.add(device.getName() + "\n"
+ device.getAddress());
}
} else {
String noDevices = getResources().getText(R.string.none_paired)
.toString();
mPairedDevicesAdapter.add(noDevices);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBtAdapter != null) {
mBtAdapter.cancelDiscovery();
}
this.unregisterReceiver(mReceiver);
}
/**
* 扫描本地可用的设备
*/
private void discoverDevice() {
setProgressBarIndeterminateVisibility(true);
setTitle(R.string.scanning);
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
//如果正在扫描,先停止扫描,再重新扫描
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
mBtAdapter.startDiscovery();
}
/**
* 监听搜索到的设备
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
LogUtils.getInstance().e(getClass(), "----ACTION_FOUND---");
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
if(!TextUtils.isEmpty(device.getName())){
if(device.getName().equals("小米手机")){
LogUtils.getInstance().e(getClass(), "----device.getName():"+device.getName());
mNewDevicesAdapter.add(device.getName() + "\n"
+ device.getAddress());
}
}
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
.equals(action)) {
LogUtils.getInstance().e(getClass(), "----ACTION_DISCOVERY_FINISHED----");
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesAdapter.getCount() == 0) {
String noDevices = getResources().getText(
R.string.none_found_device).toString();
mNewDevicesAdapter.add(noDevices);
}
}
}
};
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView> av, View v, int arg2, long arg3) {
mBtAdapter.cancelDiscovery();
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
setResult(Activity.RESULT_OK, intent);
finish();
}
};
}
下一篇将为大家带来,spp协议蓝牙的连接过程,以及通过蓝牙来实现文字聊天。
更多安卓相关信息,请扫码关注公众号:lifeAndroid