也能够说是蓝牙技术。所谓蓝牙(Bluetooth)技术,其实是一种短距离无线电技术,是由爱立信公司公司发明的。利用“蓝牙”技术,可以有效地简化掌上电脑、笔记本电脑和移动电话手机等移动通讯终端设备之间的通讯,也可以成功地简化以上这些设备与因特网Internet之间的通讯,从而使这些现代通讯设备与因特网之间的数据传输变得更加迅速高效,为无线通讯拓宽道路。html
Android 4.3(API Level 18)开始引入Bluetooth Low Energy(BLE,低功耗蓝牙)的核心功能并提供了相应的 API, 应用程序经过这些 API 扫描蓝牙设备、查询 services、读写设备的 characteristics(属性特征)等操做。java
Android BLE 使用的蓝牙协议是 GATT 协议,有关该协议的详细内容能够参见蓝牙官方文档。如下我引用一张官网的图来大概说明 Android 开发中咱们须要了解的一些 Bluetooth Low Energy 的专业术语。android
Android提供BluetoothAdapter类蓝牙通讯。经过调用建立的对象的静态方法getDefaultAdapter()。其语法以下给出。app
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
首先须要AndroidManifest.xml文件中添加操做蓝牙的权限。ide
<!--须要此权限来执行任何蓝牙通讯,如请求一个链接、接受一个链接和传输数据。--> <uses-permission android:name="android.permission.BLUETOOTH"/> <!-- //若是你想让你的应用启动设备发现或操纵蓝牙设置,必须申报bluetooth_admin许可--> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
验证蓝牙是否开启,未开启的提示开启this
if (!mBluetoothAdapter.isEnabled()){ //弹出对话框提示用户是后打开 Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enabler, REQUEST_ENABLE); }
Activity代码:spa
/** * 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(); //已搜素完成 } } }; }
获得效果图:code