android -------- 蓝牙Bluetooth

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述android -------- 蓝牙Bluetooth相关的知识,希望能为你提供帮助。
什么是蓝牙?
也可以说是蓝牙技术。所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的。利用“蓝牙”技术,能够有效地简化掌上电脑、笔记本电脑和移动电话手机等移动通信终端设备之间的通信,也能够成功地简化以上这些设备与因特网Internet之间的通信,从而使这些现代通信设备与因特网之间的数据传输变得更加迅速高效,为无线通信拓宽道路。
 
android 4.3(API Level 18)开始引入Bluetooth Low Energy(BLE,低功耗蓝牙)的核心功能并提供了相应的 API, 应用程序通过这些 API 扫描蓝牙设备、查询 services、读写设备的 characteristics(属性特征)等操作。
Android BLE 使用的蓝牙协议是 GATT 协议,有关该协议的详细内容可以参见蓝牙官方文档。以下我引用一张官网的图来大概说明 Android 开发中我们需要了解的一些 Bluetooth Low Energy 的专业术语。
 
 
Android提供BluetoothAdapter类蓝牙通信。通过调用创建的对象的静态方法getDefaultAdapter()。其语法如下给出。

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

首先需要AndroidManifest.xml文件中添加操作蓝牙的权限。
< !--需要此权限来执行任何蓝牙通信,如请求一个连接、接受一个连接和传输数据。--> < uses-permission android:name="android.permission.BLUETOOTH"/> < !-- //如果你想让你的应用启动设备发现或操纵蓝牙设置,必须申报bluetooth_admin许可--> < uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

 
验证蓝牙是否开启,未开启的提示开启
if (!mBluetoothAdapter.isEnabled()){ //弹出对话框提示用户是后打开 Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enabler, REQUEST_ENABLE); }

 
Activity代码:
 
/** * Created by zhangqie on 2017/11/28. */public class BluetoothActivity extends AppCompatActivity implements View.OnClickListener{private static final int REQUEST_ENABLE = 1; private static final String TAG = Demo1Activity.class.getName(); BluetoothAdapter mBluetoothAdapter; TextView tvDevices; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo1); initView(); }private void initView(){findViewById(R.id.btn1).setOnClickListener(this); tvDevices = (TextView) findViewById(R.id.textblue); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()){ //弹出对话框提示用户是后打开 Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enabler, REQUEST_ENABLE); //不做提示,直接打开,不建议用下面的方法,有的手机会有问题。 // mBluetoothAdapter.enable(); }showBluetooth(); }private void startSearthBltDevice() { //如果当前在搜索,就先取消搜索 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } //开启搜索 mBluetoothAdapter.startDiscovery(); }private void showBoradCast(){ // 设置广播信息过滤 IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); //每搜索到一个设备就会发送一个该广播 filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //当全部搜索完后发送该广播 filter.setPriority(Integer.MAX_VALUE); //设置优先级 // 注册蓝牙搜索广播接收者,接收并处理搜索结果 this.registerReceiver(receiver, filter); }@Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1: showBoradCast(); startSearthBltDevice(); break; } }//获取已经配对的蓝牙设备 private void showBluetooth(){ Set< BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { tvDevices.append(device.getName() + ":" + device.getAddress()); } } }/** * 定义广播接收器 */ private final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { Log.i(TAG, ":"+ device.getAddress()); tvDevices.append(device.getName() + ":"+ device.getAddress()); } } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {Toast.makeText(Demo1Activity.this,"已搜索完成",Toast.LENGTH_LONG).show(); //已搜素完成 } } }; }

【android -------- 蓝牙Bluetooth】 
 
得到效果图:
     
android -------- 蓝牙Bluetooth

文章图片

 
 

    推荐阅读