Android 低功耗蓝(1)

1:首先在程序里我们要开启蓝牙权限

低功能耗蓝牙也就是BLE, 它的角色变成了一个是中心设备(central)一个是外围设备(peripheral),中心设备就是你的手机,外围设备就是智能手环一类的东西。大于等于4.3 和 小于5.0 之间的android手机系统版本,只能作为中心设备去搜索一些其他外围设备,不能做为外围设备被其他中心设备搜索到;在 5.0以及5.0 之后,既可以作为中心设备,也可以作为外围设备。

由于其具有最大化的待机时间、快速连接和低峰值的发送和接收特性,被广泛用于智能手表、智能手环等可穿戴设备上。在安卓4.3之前,安卓平台上的BLE开发相当难搞,好在谷歌在4.3之后发布了官方的API。在安卓5.0之后又引入了新的API,而新的api没有向下兼容,所以采用5.0新API开发的APP只能在LOLLIPOP即安卓5.0及其以后的版本使用。不过,在新的系统里采用旧API开发的APP仍可使用。

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

如果你想声明你的应用程序只能在支持BLE的设备上运行,可以将下面声明包含进你的应用程序manifest文件中:

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"></uses-feature>

然而,如果你想让你的应用程序也能够在不支持BLE的设备上运行,你就应该将上面标签中的属性设置为required=”false”。然后在运行的过程中使用PackageManager.hasSystemFeature()方法来判断设备是否支持BLE:
2断设备是否支持BLE:

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
    finish();
}

3判断设备是否支持蓝牙ble
// 检查当前手机是否支持ble 蓝牙,如果不支持退出程序

if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
}

4:获取蓝牙适配器BluetoothAdapter
可以通过以下两种方式获取

final BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

5:弹出是否启用蓝牙的对话框

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_ENABLE:
        if (resultCode == Activity.REQUEST_ENABLE_BT) {
            Toast.makeText(this, "蓝牙已启用", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "蓝牙未启用", Toast.LENGTH_SHORT).show();
        }
        break;
    }
}

//也可以直接调用mBluetoothAdapter.enable(),mBluetoothAdapter.disable()来启用禁用蓝牙。不过这种方式不会弹出询问对话框
6:搜索设备

  private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD); //10秒后停止搜索
            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);//停止搜索
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值