![](https://img-cdn-tc.dcloud.net.cn/account/identicon/aec781ec67da116984ec402d34364a4e.png)
读取IC 卡的ID信息以及NFC读写IC卡扇区信息
本人第一次用hbuilderx 开发手机app 因需求,,需要读写IC卡及获取IC卡的唯一ID
在论坛转了转,参照相关的帖子,如下: 原文地址1 原文地址2以及查看了android.nfc的相关api说明.经验分享出来.
var NfcAdapter;
var NdefRecord;
var NdefMessage;
var nfcAdapter;
function listenNFCStatus() {
try {
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var Activity = plus.android.importClass('android.app.Activity');
var PendingIntent = plus.android.importClass('android.app.PendingIntent');
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
nfcAdapter = NfcAdapter.getDefaultAdapter(main);
var intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("*/*");
var intentFiltersArray = [ndef];
var techListsArray = [
["android.nfc.tech.IsoDep"],
["android.nfc.tech.NfcA"],
["android.nfc.tech.NfcB"],
["android.nfc.tech.NfcF"],
["android.nfc.tech.Nfcf"],
["android.nfc.tech.NfcV"],
["android.nfc.tech.NdefFormatable"],
["android.nfc.tech.MifareClassi"],
["android.nfc.tech.MifareUltralight"]
];
document.addEventListener("newintent",
function() {
console.error('newintent');
setTimeout(handle_nfc_data1, 1000);
}, false);
document.addEventListener("pause", function(e) {
if (nfcAdapter) {
nfcAdapter.disableForegroundDispatch(main);
console.log('pause');
}
}, false);
document.addEventListener("resume", function(e) {
if (nfcAdapter) {
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
}
}, false);
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
} catch (e) {
console.error(e);
}
}
function handle_nfc_data1() {
NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
var main = plus.android.runtimeMainActivity();
var intent = main.getIntent();
if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
if (readyWriteData) {
__write(intent);
readyWriteData = false;
} else if (readyRead) {
__read(intent);
readyRead = false;
} else if (readyGetid) {
__getId(intent);
readyGetid = false;
}
}
}
function showToast(msg) {
plus.nativeUI.toast(msg);
}
function __write(intent) {
try {
waiting.setTitle('请勿移开标签\n正在写入...');
var textBytes = plus.android.invoke(writeInfo, "getBytes");
// image/jpeg text/plain
var textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
plus.android.invoke("text/plain", "getBytes"), plus.android.invoke("", "getBytes"), textBytes);
var message = new NdefMessage([textRecord]);
var Ndef = plus.android.importClass('android.nfc.tech.Ndef');
var NdefFormatable = plus.android.importClass('android.nfc.tech.NdefFormatable');
var tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
var ndef = Ndef.get(tag);
console.log(JSON.stringify(ndef));
if (ndef != null) {
var size = message.toByteArray().length;
console.log("size=" + size);
ndef.connect();
if (!ndef.isWritable()) {
showToast("tag不允许写入");
waiting.close();
return;
}
if (ndef.getMaxSize() < size) {
showToast("文件大小超出容量");
waiting.close();
return;
}
ndef.writeNdefMessage(message);
waiting.close();
showToast("写入数据成功.");
return;
} else {
var format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
showToast("格式化tag并且写入message");
waiting.close();
return;
} catch (e) {
showToast("格式化tag失败.");
waiting.close();
return;
}
} else {
showToast("Tag不支持NDEF");
waiting.close();
return;
}
}
} catch (e) {
console.log("error=" + e);
waiting.close();
alert('写入失败');
}
}
function __read(intent) {
try {
waiting.setTitle('请勿移开标签\n正在读取数据...');
var Parcelable = plus.android.importClass("android.os.Parcelable");
var rawmsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
var records = rawmsgs[0].getRecords();
var result = records[0].getPayload();
var s = plus.android.newObject("java.lang.String", result);
waiting.close();
if (s.length > 0) {
if (typeof readAction === 'function') {
readAction(s);
}
} else {
showToast("数据为空");
}
} catch (e) {
console.log("error=" + e);
waiting.close();
alert('读取失败');
}
}
function __getId(intent) {
try {
var tag = plus.android.importClass('android.nfc.Tag');
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
var result = tag.getId();
//var id = plus.android.newObject("java.lang.String", result);
waiting.close();
if (result.length > 0) {
if (typeof GetidAction === 'function') {
GetidAction(GetIdParam, result);
}
} else {
showToast("数据为空");
}
} catch (e) {
//TODO handle the exception
console.log("error=" + e);
waiting.close();
alert('读取失败');
}
}
document.addEventListener('plusready', listenNFCStatus, false);
var waiting;
var readyWriteData = false;
var readyRead = false;
var readyGetid = false;
var readAction;
var GetidAction;
var GetIdParam;
var writeAction;
var writeInfo;
function writeData(writeValue, writefunction, ) {
readyWriteData = true;
writeInfo = writeValue;
writeAction = writefunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function readData(readfunction) {
readyRead = true;
readAction = readfunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function getId(param, getidfunction) {
readyGetid = true;
GetIdParam = param;
GetidAction = getidfunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function convertCardID(cardIdResult) {
var tmp = '';
for (var i = 0; i < cardIdResult.length; i++) {
var valueStr = parseInt(cardIdResult[i]);
if (valueStr < 0) {
tmp = tmp + (255 + valueStr + 1).toString(16).toUpperCase() + ":";
} else if (valueStr >= 0 && valueStr < 16) {
tmp = tmp + '0' + valueStr.toString(16).toUpperCase() + ":";
} else {
tmp = tmp + valueStr.toString(16).toUpperCase() + ":";
}
}
return tmp.substr(0, tmp.length - 1);
}
本人第一次用hbuilderx 开发手机app 因需求,,需要读写IC卡及获取IC卡的唯一ID
在论坛转了转,参照相关的帖子,如下: 原文地址1 原文地址2以及查看了android.nfc的相关api说明.经验分享出来.
var NfcAdapter;
var NdefRecord;
var NdefMessage;
var nfcAdapter;
function listenNFCStatus() {
try {
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var Activity = plus.android.importClass('android.app.Activity');
var PendingIntent = plus.android.importClass('android.app.PendingIntent');
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
nfcAdapter = NfcAdapter.getDefaultAdapter(main);
var intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("*/*");
var intentFiltersArray = [ndef];
var techListsArray = [
["android.nfc.tech.IsoDep"],
["android.nfc.tech.NfcA"],
["android.nfc.tech.NfcB"],
["android.nfc.tech.NfcF"],
["android.nfc.tech.Nfcf"],
["android.nfc.tech.NfcV"],
["android.nfc.tech.NdefFormatable"],
["android.nfc.tech.MifareClassi"],
["android.nfc.tech.MifareUltralight"]
];
document.addEventListener("newintent",
function() {
console.error('newintent');
setTimeout(handle_nfc_data1, 1000);
}, false);
document.addEventListener("pause", function(e) {
if (nfcAdapter) {
nfcAdapter.disableForegroundDispatch(main);
console.log('pause');
}
}, false);
document.addEventListener("resume", function(e) {
if (nfcAdapter) {
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
}
}, false);
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
} catch (e) {
console.error(e);
}
}
function handle_nfc_data1() {
NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
var main = plus.android.runtimeMainActivity();
var intent = main.getIntent();
if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
if (readyWriteData) {
__write(intent);
readyWriteData = false;
} else if (readyRead) {
__read(intent);
readyRead = false;
} else if (readyGetid) {
__getId(intent);
readyGetid = false;
}
}
}
function showToast(msg) {
plus.nativeUI.toast(msg);
}
function __write(intent) {
try {
waiting.setTitle('请勿移开标签\n正在写入...');
var textBytes = plus.android.invoke(writeInfo, "getBytes");
// image/jpeg text/plain
var textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
plus.android.invoke("text/plain", "getBytes"), plus.android.invoke("", "getBytes"), textBytes);
var message = new NdefMessage([textRecord]);
var Ndef = plus.android.importClass('android.nfc.tech.Ndef');
var NdefFormatable = plus.android.importClass('android.nfc.tech.NdefFormatable');
var tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
var ndef = Ndef.get(tag);
console.log(JSON.stringify(ndef));
if (ndef != null) {
var size = message.toByteArray().length;
console.log("size=" + size);
ndef.connect();
if (!ndef.isWritable()) {
showToast("tag不允许写入");
waiting.close();
return;
}
if (ndef.getMaxSize() < size) {
showToast("文件大小超出容量");
waiting.close();
return;
}
ndef.writeNdefMessage(message);
waiting.close();
showToast("写入数据成功.");
return;
} else {
var format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
showToast("格式化tag并且写入message");
waiting.close();
return;
} catch (e) {
showToast("格式化tag失败.");
waiting.close();
return;
}
} else {
showToast("Tag不支持NDEF");
waiting.close();
return;
}
}
} catch (e) {
console.log("error=" + e);
waiting.close();
alert('写入失败');
}
}
function __read(intent) {
try {
waiting.setTitle('请勿移开标签\n正在读取数据...');
var Parcelable = plus.android.importClass("android.os.Parcelable");
var rawmsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
var records = rawmsgs[0].getRecords();
var result = records[0].getPayload();
var s = plus.android.newObject("java.lang.String", result);
waiting.close();
if (s.length > 0) {
if (typeof readAction === 'function') {
readAction(s);
}
} else {
showToast("数据为空");
}
} catch (e) {
console.log("error=" + e);
waiting.close();
alert('读取失败');
}
}
function __getId(intent) {
try {
var tag = plus.android.importClass('android.nfc.Tag');
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
var result = tag.getId();
//var id = plus.android.newObject("java.lang.String", result);
waiting.close();
if (result.length > 0) {
if (typeof GetidAction === 'function') {
GetidAction(GetIdParam, result);
}
} else {
showToast("数据为空");
}
} catch (e) {
//TODO handle the exception
console.log("error=" + e);
waiting.close();
alert('读取失败');
}
}
document.addEventListener('plusready', listenNFCStatus, false);
var waiting;
var readyWriteData = false;
var readyRead = false;
var readyGetid = false;
var readAction;
var GetidAction;
var GetIdParam;
var writeAction;
var writeInfo;
function writeData(writeValue, writefunction, ) {
readyWriteData = true;
writeInfo = writeValue;
writeAction = writefunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function readData(readfunction) {
readyRead = true;
readAction = readfunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function getId(param, getidfunction) {
readyGetid = true;
GetIdParam = param;
GetidAction = getidfunction;
waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
}
function convertCardID(cardIdResult) {
var tmp = '';
for (var i = 0; i < cardIdResult.length; i++) {
var valueStr = parseInt(cardIdResult[i]);
if (valueStr < 0) {
tmp = tmp + (255 + valueStr + 1).toString(16).toUpperCase() + ":";
} else if (valueStr >= 0 && valueStr < 16) {
tmp = tmp + '0' + valueStr.toString(16).toUpperCase() + ":";
} else {
tmp = tmp + valueStr.toString(16).toUpperCase() + ":";
}
}
return tmp.substr(0, tmp.length - 1);
}
收起阅读 »
![](https://img-cdn-tc.dcloud.net.cn/account/identicon/ab453b87c0016a6737b03312a28a84d8.png)
关于.9.png经验分享
以前没搞过.9.png,这个东东初学者真的很难理解,昨天仔细研究了一下,算是搞明白了,所以分享一下使用心得。
1、首先读一下这篇文章Android设计中的.9.png图片
2、上面说得很清楚这个.9.png能做到什么,但是怎么操作细节却没讲清楚,下面是重点
a、工具是指的draw9patch.bat工具,android SDK上自带的,只要安装过SDK,应该能搜索到
b、先勾上Show patches方便看结果,粉色为拉伸区域,表色是固定区域
c、工具左边是编辑区,右边是预览区(从上到下3个预览结果,1纵向拉伸效果,2横向拉伸效果,3放大拉伸效果)
d、画黑边,在图像区域外点击拖动鼠标,每条黑边都是一个矩形
e、擦除黑边,还是在图像区域外,拖动这个黑边对应矩形的边界,直到矩形的高或宽为0,就算擦除了
f、要按shift键,只能在边的一个像素上操作(先放大图像,不然点不到一个像素的),可以把一个矩形切成2个
g、Show content勾上后可以在预览区看到文字在显示的区域(淡紫色区域)
3、编辑.9.png如果没弄对,本地打包会失败,云打包会不会失败暂时不知道
以前没搞过.9.png,这个东东初学者真的很难理解,昨天仔细研究了一下,算是搞明白了,所以分享一下使用心得。
1、首先读一下这篇文章Android设计中的.9.png图片
2、上面说得很清楚这个.9.png能做到什么,但是怎么操作细节却没讲清楚,下面是重点
a、工具是指的draw9patch.bat工具,android SDK上自带的,只要安装过SDK,应该能搜索到
b、先勾上Show patches方便看结果,粉色为拉伸区域,表色是固定区域
c、工具左边是编辑区,右边是预览区(从上到下3个预览结果,1纵向拉伸效果,2横向拉伸效果,3放大拉伸效果)
d、画黑边,在图像区域外点击拖动鼠标,每条黑边都是一个矩形
e、擦除黑边,还是在图像区域外,拖动这个黑边对应矩形的边界,直到矩形的高或宽为0,就算擦除了
f、要按shift键,只能在边的一个像素上操作(先放大图像,不然点不到一个像素的),可以把一个矩形切成2个
g、Show content勾上后可以在预览区看到文字在显示的区域(淡紫色区域)
3、编辑.9.png如果没弄对,本地打包会失败,云打包会不会失败暂时不知道
收起阅读 »![](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/000/83/36/63_avatar_mid.jpg?v=0)
uni-app蓝牙开锁篇
uni-app的api和微信的api其实很相似,用法一样,在这里奉上我之前在项目中实现蓝牙开锁的代码,我会说明每一步的步骤,哪个步骤用哪个api,每个api的详细用法可以去uni-app官网参考文档
-
蓝牙整个步骤:1初始化蓝牙,2开始搜寻附近的蓝牙外围设备,3监听寻找到新设备的事件,4搜寻到需要的蓝牙,停止搜寻附近的蓝牙外围设备,5连接低功耗蓝牙设备,6获取蓝牙设备所有服务(service),7获取蓝牙设备某个服务中所有特征值(characteristic),8启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用,9向低功耗蓝牙设备特征值中写入二进制数据。
uni.openBluetoothAdapter({//首先初始化蓝牙 success(res) { console.log(JSON.stringify(res)) uni.startBluetoothDevicesDiscovery({//这里是开启蓝牙搜寻 success: (res) => { console.log('startBluetoothDevicesDiscovery success', res) uni.onBluetoothDeviceFound((res) => {//这一步是监听返回的蓝牙设备 console.log(JSON.stringify(res)) res.devices.forEach(device => {//这一步就是去筛选找到的蓝牙中,有没有你匹配的名称 console.log(JSON.stringify(device)) if (device.name == 'XiaoanTech') { this.DeviceID = device.deviceId let DeviceID = device.deviceId//这里是拿到的uuid uni.stopBluetoothDevicesDiscovery({//当找到匹配的蓝牙后就关掉蓝牙搜寻,因为蓝牙搜寻很耗性能 success(res) { console.log(JSON.stringify(res)) } }) console.log(DeviceID) uni.createBLEConnection({//连接低功耗蓝牙设备 deviceId:DeviceID,//传入刚刚获取的uuid success(res) { console.log(JSON.stringify(res)) //uni.getConnectedBluetoothDevices({ //success(res) { //console.log(JSON.stringify(res)) //} //}) setTimeout(function(){//这里为什么要用setTimeout呢,等等下面会解释 uni.getBLEDeviceServices({//获取蓝牙设备所有服务 deviceId:DeviceID, success(res) {//为什么要用延时,因为不用延时就拿不到所有的服务,在上一步,连接低功耗蓝牙 //设备的时候,需要一个600-1000毫秒的时间后,再去获取设备所有服务,不给延时就会一直返回错误码10004 console.log(JSON.stringify(res)) uni.getBLEDeviceCharacteristics({//获取蓝牙设备某个服务中所有特征值 deviceId:DeviceID, serviceId:this.ServiceUUID,//这个serviceId可以在上一步获取中拿到,也可以在 //蓝牙文档中(硬件的蓝牙文档)拿到,我这里是通过文档直接赋值上去的,一般有两个,一个是收的uuid,一个是发的uuid,我们这边是发 success(res) { console.log(JSON.stringify(res)) uni.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId:DeviceID, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId:this.ServiceUUID, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId:self.characteristicId, success(res) { console.log('notifyBLECharacteristicValueChange success', res.errMsg) uni.showToast({ title: '开启蓝牙连接', duration: 2000 }); }, fail(res) { console.log(JSON.stringify(res)) } }) }, fail(res){ console.log(JSON.stringify(res)) } }) }, fail(res){ console.log(JSON.stringify(res)) } }) },1000) }, fail(res) { console.log(res) } }) } }) }) } }) }, fail(res) { console.log(res) if (res.errCode == 10001) { uni.showToast({ title: '蓝牙未打开', duration: 2000, }) } else { uni.showToast({ title: res.errMsg, duration: 2000, }) } } })
-
蓝牙连接成功后就是发送指令了,发送二进制数据
SendChange: function () {//开锁 var self = this // 向蓝牙设备发送一个0x00的16进制数据 let buffer = new ArrayBuffer(8) let dataView = new DataView(buffer) dataView.setUint8(0, 0x20)//开锁指令 dataView.setUint8(1, 0x05)//字节 dataView.setUint8(2, 0x0A)//指令 dataView.setUint8(3, 0x0A)//指令 dataView.setUint8(4, 0x05)//指令 dataView.setUint8(5, 0x05)//指令 dataView.setUint8(6, 0x00)//指令 //算法,逢10进1,A到F(或a~f)表示,其中:A~F表示10~15 //20等于32,32+05+10+10+5+5+0 = 67 dataView.setUint8(7, 0x43) uni.writeBLECharacteristicValue({ // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId:self.DeviceID, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId:self.ServiceUUID, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId:self.characteristicId, // 这里的value是ArrayBuffer类型 value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) uni.showToast({ title: '开锁', duration: 2000 }); }, fail(res) { console.log(JSON.stringify(res)) console.log(JSON.stringify(buffer)) } }) }, CloseChange: function () {//关锁 var self = this // 向蓝牙设备发送一个0x00的16进制数据 let buffer = new ArrayBuffer(8) let dataView = new DataView(buffer) dataView.setUint8(0, 0x20) dataView.setUint8(1, 0x05) dataView.setUint8(2, 0x0A) dataView.setUint8(3, 0x0A) dataView.setUint8(4, 0x05) dataView.setUint8(5, 0x05) dataView.setUint8(6, 0x01) dataView.setUint8(7, 0x44) uni.writeBLECharacteristicValue({ // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId:self.DeviceID, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId:self.ServiceUUID, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId:self.characteristicId, // 这里的value是ArrayBuffer类型 value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) uni.showToast({ title: '关锁', duration: 2000 }); }, fail(res) { console.log(JSON.stringify(res)) console.log(JSON.stringify(buffer)) } }) },
文章有缺陷,因为蓝牙的指令文档被我搞不见了,只能通过理解写给大家看,希望能帮到大家,谢谢
uni-app的api和微信的api其实很相似,用法一样,在这里奉上我之前在项目中实现蓝牙开锁的代码,我会说明每一步的步骤,哪个步骤用哪个api,每个api的详细用法可以去uni-app官网参考文档
-
蓝牙整个步骤:1初始化蓝牙,2开始搜寻附近的蓝牙外围设备,3监听寻找到新设备的事件,4搜寻到需要的蓝牙,停止搜寻附近的蓝牙外围设备,5连接低功耗蓝牙设备,6获取蓝牙设备所有服务(service),7获取蓝牙设备某个服务中所有特征值(characteristic),8启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用,9向低功耗蓝牙设备特征值中写入二进制数据。
uni.openBluetoothAdapter({//首先初始化蓝牙 success(res) { console.log(JSON.stringify(res)) uni.startBluetoothDevicesDiscovery({//这里是开启蓝牙搜寻 success: (res) => { console.log('startBluetoothDevicesDiscovery success', res) uni.onBluetoothDeviceFound((res) => {//这一步是监听返回的蓝牙设备 console.log(JSON.stringify(res)) res.devices.forEach(device => {//这一步就是去筛选找到的蓝牙中,有没有你匹配的名称 console.log(JSON.stringify(device)) if (device.name == 'XiaoanTech') { this.DeviceID = device.deviceId let DeviceID = device.deviceId//这里是拿到的uuid uni.stopBluetoothDevicesDiscovery({//当找到匹配的蓝牙后就关掉蓝牙搜寻,因为蓝牙搜寻很耗性能 success(res) { console.log(JSON.stringify(res)) } }) console.log(DeviceID) uni.createBLEConnection({//连接低功耗蓝牙设备 deviceId:DeviceID,//传入刚刚获取的uuid success(res) { console.log(JSON.stringify(res)) //uni.getConnectedBluetoothDevices({ //success(res) { //console.log(JSON.stringify(res)) //} //}) setTimeout(function(){//这里为什么要用setTimeout呢,等等下面会解释 uni.getBLEDeviceServices({//获取蓝牙设备所有服务 deviceId:DeviceID, success(res) {//为什么要用延时,因为不用延时就拿不到所有的服务,在上一步,连接低功耗蓝牙 //设备的时候,需要一个600-1000毫秒的时间后,再去获取设备所有服务,不给延时就会一直返回错误码10004 console.log(JSON.stringify(res)) uni.getBLEDeviceCharacteristics({//获取蓝牙设备某个服务中所有特征值 deviceId:DeviceID, serviceId:this.ServiceUUID,//这个serviceId可以在上一步获取中拿到,也可以在 //蓝牙文档中(硬件的蓝牙文档)拿到,我这里是通过文档直接赋值上去的,一般有两个,一个是收的uuid,一个是发的uuid,我们这边是发 success(res) { console.log(JSON.stringify(res)) uni.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId:DeviceID, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId:this.ServiceUUID, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId:self.characteristicId, success(res) { console.log('notifyBLECharacteristicValueChange success', res.errMsg) uni.showToast({ title: '开启蓝牙连接', duration: 2000 }); }, fail(res) { console.log(JSON.stringify(res)) } }) }, fail(res){ console.log(JSON.stringify(res)) } }) }, fail(res){ console.log(JSON.stringify(res)) } }) },1000) }, fail(res) { console.log(res) } }) } }) }) } }) }, fail(res) { console.log(res) if (res.errCode == 10001) { uni.showToast({ title: '蓝牙未打开', duration: 2000, }) } else { uni.showToast({ title: res.errMsg, duration: 2000, }) } } })
-
蓝牙连接成功后就是发送指令了,发送二进制数据
SendChange: function () {//开锁 var self = this // 向蓝牙设备发送一个0x00的16进制数据 let buffer = new ArrayBuffer(8) let dataView = new DataView(buffer) dataView.setUint8(0, 0x20)//开锁指令 dataView.setUint8(1, 0x05)//字节 dataView.setUint8(2, 0x0A)//指令 dataView.setUint8(3, 0x0A)//指令 dataView.setUint8(4, 0x05)//指令 dataView.setUint8(5, 0x05)//指令 dataView.setUint8(6, 0x00)//指令 //算法,逢10进1,A到F(或a~f)表示,其中:A~F表示10~15 //20等于32,32+05+10+10+5+5+0 = 67 dataView.setUint8(7, 0x43) uni.writeBLECharacteristicValue({ // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId:self.DeviceID, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId:self.ServiceUUID, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId:self.characteristicId, // 这里的value是ArrayBuffer类型 value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) uni.showToast({ title: '开锁', duration: 2000 }); }, fail(res) { console.log(JSON.stringify(res)) console.log(JSON.stringify(buffer)) } }) }, CloseChange: function () {//关锁 var self = this // 向蓝牙设备发送一个0x00的16进制数据 let buffer = new ArrayBuffer(8) let dataView = new DataView(buffer) dataView.setUint8(0, 0x20) dataView.setUint8(1, 0x05) dataView.setUint8(2, 0x0A) dataView.setUint8(3, 0x0A) dataView.setUint8(4, 0x05) dataView.setUint8(5, 0x05) dataView.setUint8(6, 0x01) dataView.setUint8(7, 0x44) uni.writeBLECharacteristicValue({ // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId:self.DeviceID, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId:self.ServiceUUID, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId:self.characteristicId, // 这里的value是ArrayBuffer类型 value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) uni.showToast({ title: '关锁', duration: 2000 }); }, fail(res) { console.log(JSON.stringify(res)) console.log(JSON.stringify(buffer)) } }) },
文章有缺陷,因为蓝牙的指令文档被我搞不见了,只能通过理解写给大家看,希望能帮到大家,谢谢
![](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/000/63/49/38_avatar_mid.jpg?v=0)
关跳转到新页面并关闭当前页(不闪屏)
在你需要的地方执行close方法就可以了 A页面→B页面(close)→C页面 C页面(mui.back())→A页面
我自己加了延时关闭 可以根据自己情况调整
function close() {
if(!this.isBrowser) {
var ws = plus.webview.currentWebview();
plus.webview.hide(ws);
setTimeout(function() {
plus.webview.close(ws);
}, 1000)
} else {
history.back();
}
}
在你需要的地方执行close方法就可以了 A页面→B页面(close)→C页面 C页面(mui.back())→A页面
我自己加了延时关闭 可以根据自己情况调整
function close() {
if(!this.isBrowser) {
var ws = plus.webview.currentWebview();
plus.webview.hide(ws);
setTimeout(function() {
plus.webview.close(ws);
}, 1000)
} else {
history.back();
}
}
收起阅读 »
![](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/000/80/38/11_avatar_mid.jpg?v=0)
仿qq阅读页模板修改版~点击任意地方展开收起上下悬浮菜单
演示图片
需要演示地址和模板下载的请访问
https://jgo.wodemo.net/entry/523537
免费获取和下载啦。
本人自己修改的 QQ阅读页模板啦。
点击任意地方展开~顶部+底部悬浮
再次点击任意地方收起~顶部+底部悬浮
如果任何不懂,欢迎联系qq 1274003856
本模板可以适用于~影视软件开发模板
等各种软件模板开发的。
![](https://img-cdn-tc.dcloud.net.cn/account/identicon/240e45a3c6b69162d8123b730b7f1f74.png)
uni-app h5页面在chrome浏览器上跨域问题,不需要安装插件,只需要在终端输入一行代码解决
终端里输入命令:
open -a "/Applications/Google Chrome.app" --args --disable-web-security --user-data-dir=/Users/你电脑的名称/chromeDevUserData/
此方法可以解决chrome跨域,但有个问题每次关闭chrome后又会出现跨域,需要终端里重新输这条命令。
终端里输入命令:
open -a "/Applications/Google Chrome.app" --args --disable-web-security --user-data-dir=/Users/你电脑的名称/chromeDevUserData/
此方法可以解决chrome跨域,但有个问题每次关闭chrome后又会出现跨域,需要终端里重新输这条命令。
![](https://img-cdn-tc.dcloud.net.cn/account/identicon/c593795d9ab6944ae362ad6e310abe81.png)
uni-app ios 苹果真机运行
博客地址:https://www.cnblogs.com/yunsun/p/11506444.html
博客地址:https://www.cnblogs.com/yunsun/p/11506444.html
![](https://img-cdn-tc.dcloud.net.cn/account/identicon/ce37cc606fad68bb1c9b48431cdae81d.png)
ipa上传反馈修改info.plist用途字符串问题方案
很多开发者上传ipa后,收到苹果的反馈邮件说请修改应用程序info.plist文件中相关用途字符串!
意思就是需要对请求的权限进行详细说明,比如使用到了定位,相册,通讯录等权限,要把为什么使用这些权限做下详细描述!
反馈翻译截图例子!
权限描述举例说明
比如一个外卖应用,获取定位后需要展示附近的美食信息。那么,相应的定位权限描述,应当是类似“获取定位信息用于为用户提供附近的美食信息”这样的描述。
而不应当是,“获取用户当前位置信息”这种没有明确描述定位用处的信息。
HBuilderX开发工具修改入口!
打开manifest.json文件
很多开发者上传ipa后,收到苹果的反馈邮件说请修改应用程序info.plist文件中相关用途字符串!
意思就是需要对请求的权限进行详细说明,比如使用到了定位,相册,通讯录等权限,要把为什么使用这些权限做下详细描述!
反馈翻译截图例子!
权限描述举例说明
比如一个外卖应用,获取定位后需要展示附近的美食信息。那么,相应的定位权限描述,应当是类似“获取定位信息用于为用户提供附近的美食信息”这样的描述。
而不应当是,“获取用户当前位置信息”这种没有明确描述定位用处的信息。
HBuilderX开发工具修改入口!
打开manifest.json文件
![](http://img-cdn-tc.dcloud.net.cn/uploads/avatar/000/32/67/66_avatar_mid.jpg?v=0)
监听所有应用推送信息
监听所有应用推送信息:https://ext.dcloud.net.cn/plugin?id=756
监听所有应用推送信息:https://ext.dcloud.net.cn/plugin?id=756