Android 5.0(API 21)之前不能当成外设(蓝牙耳机、音响等)来使用,只能作为中心即主机
并不是Android 5.0的系统就可以支持BLE Peripheral,这个和硬件也是有关系的,谷歌从ANdroid 5.0系统SDK已经开始支持check手机是否支持BLE Peripheral
声明蓝牙开发权限
1 2 3 4 5 6
| <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
|
检查是否支持BLE、BLE Peripheral
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
private void init() { if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, "BLE not supported", Toast.LENGTH_LONG).show();
}
mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_LONG).show(); }
if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
if (!mBluetoothAdapter.isMultipleAdvertisementSupported()) { Toast.makeText(this, "This device not support peripheral", Toast.LENGTH_SHORT).show(); }
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); if (mBluetoothLeAdvertiser == null) { Toast.makeText(this, "This device not support peripheral", Toast.LENGTH_SHORT).show(); } }
|
准备发送广播
发送广播
1
| mBluetoothLeAdvertiser.startAdvertising(createAdvSettings(true, 0), createAdvertiseData(), mAdvertiseCallback);
|
广播设置参数AdvertiseSettings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static AdvertiseSettings createAdvSettings(boolean connectable, int timeoutMillis) { AdvertiseSettings.Builder mSettingsbuilder = new AdvertiseSettings.Builder(); mSettingsbuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED); mSettingsbuilder.setConnectable(connectable); mSettingsbuilder.setTimeout(timeoutMillis); mSettingsbuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH); AdvertiseSettings mAdvertiseSettings = mSettingsbuilder.build(); if(mAdvertiseSettings == null){ if(D){ Toast.makeText(mContext, "mAdvertiseSettings == null", Toast.LENGTH_LONG).show(); Log.e(TAG,"mAdvertiseSettings == null"); } } return mAdvertiseSettings; }
|
广播数据AdvertiseData
1 2 3 4 5 6 7 8 9 10 11 12
| public static AdvertiseData createAdvertiseData(){ AdvertiseData.Builder mDataBuilder = new AdvertiseData.Builder(); mDataBuilder.addServiceUuid(ParcelUuid.fromString(HEART_RATE_SERVICE)); AdvertiseData mAdvertiseData = mDataBuilder.build(); if(mAdvertiseData==null){ if(D){ Toast.makeText(mContext, "mAdvertiseSettings == null", Toast.LENGTH_LONG).show(); Log.e(TAG,"mAdvertiseSettings == null"); } } return mAdvertiseData; }
|
广播回调Callback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); if (settingsInEffect != null) { Log.d(TAG, "onStartSuccess TxPowerLv=" + settingsInEffect.getTxPowerLevel() + " mode=" + settingsInEffect.getMode() + " timeout=" + settingsInEffect.getTimeout()); } else { Log.e(TAG, "onStartSuccess, settingInEffect is null"); } showText("1. initGattServer Success"); Log.e(TAG,"onStartSuccess settingsInEffect" + settingsInEffect); } @Override public void onStartFailure(int errorCode) { super.onStartFailure(errorCode); showText("1. initGattServer failed"); } };
|
广播关闭
1 2 3 4 5 6
| private void stopAdvertise() { if (mBluetoothLeAdvertiser != null) { mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback); mBluetoothLeAdvertiser = null; } }
|
附上代码地址: https://github.com/xmaihh/Android-BLEPeripheral