代码拉取完成,页面将自动刷新
1.在项目根目录下的build.gradle文件中,
allprojects {
repositories {
maven {
url 'https://s01.oss.sonatype.org/content/repositories/releases/'
}
}
}
2.在entry模块的build.gradle文件中,
dependencies {
implementation('com.gitee.chinasoft_ohos:okble:1.0.0')
......
}
在sdk6,DevEco Studio 2.2 Beta1下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下
OKBLEScanManager scanManager = new OKBLEScanManager(this);
scanManager.setScanCallBack(scanCallBack);
DeviceScanCallBack scanCallBack = new DeviceScanCallBack() {
@Override
public void onBLEDeviceScan(BLEScanResult device, int rssi) {
LogUtils.e(" scan:"+device.toString());
}
@Override
public void onFailed(int code) {
switch (code) {
case DeviceScanCallBack.SCAN_FAILED_BLE_NOT_SUPPORT:
showToast("该设备不支持BLE");
break;
case DeviceScanCallBack.SCAN_FAILED_BLUETOOTH_DISABLE:
showToast("请打开手机蓝牙");
break;
case DeviceScanCallBack.SCAN_FAILED_LOCATION_PERMISSION_DISABLE:
showToast("请授予位置权限以扫描周围的蓝牙设备");
break;
case DeviceScanCallBack.SCAN_FAILED_LOCATION_PERMISSION_DISABLE_FOREVER:
showToast("位置权限被您永久拒绝,请在设置里授予位置权限以扫描周围的蓝牙设备");
break;
}
}
@Override
public void onStartSuccess() {
}
};
OKBLEDevice okbleDevice=new OKBLEDeviceImp(mContext,bleScanResult);
//okbleDevice=new OKBLEDeviceImp(mContext);
//okbleDevice.setBluetoothDevice(mBluetoothDevice);
okbleDevice.addDeviceListener(this);
okbleDevice.connect(true);//true表示连接断开后OKBLE的会自动重连
okbleDevice.disConnect(false); //false表示断开后不需要OKBLE的自动重连; disConnect断开后,可以使用okbleDevice.connect()重新连接回来
okbleDevice.remove(); //remove会清除连接的外设信息; 重新连接前需要重新调用setBleScanResult/setBluetoothDevice 来设置外设信息
// Read
application.okbleDevice.addReadOperation(characteristicModel.getUuid(), new OKBLEOperation.ReadOperationListener() {
@Override
public void onReadValue(final byte[] value) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog("onReadValue:" + OKBLEDataUtils.BytesToHexString(value) + " (" + new String(value) + ")");
}
});
}
@Override
public void onFail(int code, final String errMsg) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog("read onFail:" + errMsg);
}
});
}
@Override
public void onExecuteSuccess(OKBLEOperation.OperationType type) {
}
});
// Write
application.okbleDevice.addWriteOperation(characteristicModel.getUuid(), value, new OKBLEOperation.WriteOperationListener() {
@Override
public void onWriteValue(final byte[] byteValue) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog(" onWriteValue:" + byteValue);
}
});
}
@Override
public void onFail(int code, final String errMsg) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog("write onFail:" + errMsg);
}
});
}
@Override
public void onExecuteSuccess(OKBLEOperation.OperationType type) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog("write value execute success value length:" + OKBLEDataUtils.hexStringToBytes(value).length + " value:" + value);
}
});
}
});
// Notify/Indicate
application.okbleDevice.addNotifyOrIndicateOperation(characteristicModel.getUuid(), true, new OKBLEOperation.NotifyOrIndicateOperationListener() {
@Override
public void onNotifyOrIndicateComplete() {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog("onNotifyOrIndicateComplete");
}
});
}
@Override
public void onFail(int code, final String errMsg) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
addLog("NotifyOrIndicate onFail:" + errMsg);
}
});
}
@Override
public void onExecuteSuccess(OKBLEOperation.OperationType type) {
operationType[0] = type;
}
});
final int sendInterval=50;//每个包之间的发送间隔,有些手机会因为发送太快而导致蓝牙奔溃,OAD失败率很高,可以适当增大,如80-100;
okbleDevice.setOperationInterval(sendInterval);
byte[] oadValues=loadBytesFromFile(filePath);
final int blockSize=20;//表示一个包发送20个字节
final int blockCount= (int) Math.ceil(oadValues.length*1.0f/blockSize);//发送的总包数
percent=0;
for (int i=0;i<blockCount;i++){
byte[] value=new byte[blockSize];
System.arraycopy(oadValues,i*blockSize,value,0,blockSize);
okbleDevice.addWriteOperation(OAD_WRITE_UUID, value, new OKBLEOperation.WriteOperationListener() {
@Override
public void onWriteValue(byte[] value) {
percent++;
float progress=percent*1.0f/blockCount;
int leftSeconds= (int) ((sendInterval*blockCount)/1000*(1-progress));
HiLog.error(LABEL, "OAD 进度:"+progress+" 剩余时间:"+leftSeconds +"秒");
}
@Override
public void onFail(int code, String errMsg) {
HiLog.error(LABEL, " OAD failed");
okbleDevice.clearOperations();
break;
}
@Override
public void onExecuteSuccess(OKBLEOperation.OperationType type) {
}
});
}
OKBLEAdvertiseManager okbleAdvertiseManager;
OKBLEServerDevice serverDevice;
okbleAdvertiseManager=new OKBLEAdvertiseManager(this);
serverDevice=new OKBLEServerDeviceImp(this);
OKBLEAdvertiseSettings settings= new OKBLEAdvertiseSettings.Builder().setConnectable(true).build();
OKBLEAdvertiseData.Builder dataBuilder = new OKBLEAdvertiseData.Builder().setIncludeDeviceName(true);
//开启广播
okbleAdvertiseManager.startAdvertising(settings, okbleAdvertiseData, new OKBLEAdvertiseCallback() {
@Override
public void onStartSuccess() {
LogUtils.e("---onStartSuccess ---");
new EventHandler(EventRunner.getMainEventRunner()).postTask(() ->
ToastUtil.toast(mContext,"Advertising Success");
configServer();//配置service 和characteristic
}
@Override
public void onStartFailure(int errorCode, String errMsg) {
LogUtils.e("---onStartFailure errMsg:" + errMsg);
int screenWidht = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes().width;
DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
.parse(ResourceTable.Layout_layout_toast, null, false);
Text msg_toast = (Text) toastLayout.findComponentById(ResourceTable.Id_msg_toast);
msg_toast.setText("Advertising Failed:"+errMsg);
new ToastDialog(mContext) .setComponent(toastLayout)
.setSize((int) (screenWidht*0.7), DirectionalLayout.LayoutConfig.MATCH_CONTENT).show();
}
});
private void configServer() {
OKBLEServiceModel serviceModel = new OKBLEServiceModel(CommonUUIDUtils.createCompleteUUIDByShortUUID("fff0"));
OKBLECharacteristicModel characteristicModel = new OKBLECharacteristicModel(CommonUUIDUtils.createCompleteUUIDByShortUUID("fff1"));
characteristicModel.setCanWrite(true);
characteristicModel.setCanNotify(true);
characteristicModel.setCanRead(true);
OKBLECharacteristicModel characteristicModel_2 = new OKBLECharacteristicModel(CommonUUIDUtils.createCompleteUUIDByShortUUID("fff2"));
characteristicModel_2.setCanWriteNoResponse(true);
List<OKBLECharacteristicModel> characteristicModels = new ArrayList<>();
characteristicModels.add(characteristicModel);
characteristicModels.add(characteristicModel_2);
serverDevice.addCharacteristic(characteristicModels, serviceModel, new OKBLEServerOperation.BLEServerOperationListener() {
@Override
public void onAddCharacteristicFailed(int errorCode, String errorMsg) {
LogUtils.e("onAddCharacteristicFailed:" + errorMsg);
}
@Override
public void onAddCharacteristicSuccess() {
LogUtils.e("onAddCharacteristicSuccess");
}
});
}
OKBLEBeaconScanManager scanManager;
scanManager=new OKBLEBeaconScanManager(this);
scanManager.setBeaconScanCallback(scanCallBack);
OKBLEBeaconManager.OKBLEBeaconScanCallback scanCallBack = new OKBLEBeaconManager.OKBLEBeaconScanCallback() {
@Override
public void onScanBeacon(OKBLEBeacon beacon) {
refreshBtn.setEnabled(true);
int value[] = scanedResults.put(beacon.getIdentifier(), beacon);
new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
@Override
public void run() {
if (value[1] == 1) {
//这是新增数据
provider.notifyDataChanged();
} else {
//这是重复数据,刷新rssi
int index = value[0];
updatePosition(index);
}
}
});
}
};
OKBLEBeBeaconManager beBeaconManager;
beBeaconManager = new OKBLEBeBeaconManager(this);
beBeaconManager.setOKBLEBeBeaconListener(startBeaconListener);
String uuid ="12345678-1234-1234-1234-1234567890ab";
int major=1;
int minor=2;
beBeaconManager.startIBeacon(uuid, major, minor);
OKBLEBeBeaconManager.OKBLEStartBeaconListener startBeaconListener = new OKBLEBeBeaconManager.OKBLEStartBeaconListener() {
@Override
public void onStartSuccess() {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
ToastUtil.toast(mContext,"start success");
}
});
}
@Override
public void onStartFailure(String errMsg) {
eventHandler.postTask(new Runnable() {
@Override
public void run() {
ToastUtil.toast(mContext,"start failed:" + errMsg);
}
});
}
};
OKBLEBeaconManager beaconManager=new OKBLEBeaconManager(this);
beaconManager.setRegionListener(this);
String uuid ="12345678-1234-1234-1234-1234567890ab";
int major=1;
int minor=2;
OKBLEBeaconRegion okbleBeaconRegion=OKBLEBeaconRegion.getInstance(uuid,major,minor);
//OKBLEBeaconRegion okbleBeaconRegion=OKBLEBeaconRegion.getInstance(uuid,major);
//OKBLEBeaconRegion okbleBeaconRegion=OKBLEBeaconRegion.getInstance(uuid);
beaconManager.startMonitoringForRegion(okbleBeaconRegion);
@Override
public void onEnterBeaconRegion(OKBLEBeaconRegion beaconRegion) {
}
@Override
public void onExitBeaconRegion(OKBLEBeaconRegion beaconRegion) {
}
CodeCheck代码测试无异常
CloudTest代码测试无异常
病毒安全检测通过
当前版本demo功能与原组件基本无差异
Copyright 2018 a1anwang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型