
#include <reg51h>
#include <intrinsh>
#define uchar unsigned char
uchar data tab[10];//将要提取的数据装入这个数组,数组元素为10个,不够时自己改一下
main()
{
uchar i;
SCON = 0x50;
TMOD = 0x20;
TH1 = 0x;//串口波特率设置,需要提供单片机的时钟频率。
TL1 = 0x;
TR1 = 1;
while(1)
{
for(i = 0;i<8;i++)//等待串口发送的前8个字节数据并丢弃,这是最简单方法,当然也有复杂的
{
while(!RI);//等待接收串口数据
RI =0;//清接收标志
}
for(i = 0;i<10;i++)
{
while(!RI);
tab[i] = SBUF;//将提起的数据载入数组
RI = 0;
if(!SBUF)brwak;//如果接收的数据为00,结束提取,通过变量“i”可知道所提取数据的字节数
}
for(i = 0;i<4;i++ )//等待最后4个字节接收并丢弃数据
{
while(!RI);
RI = 0;
}
}
}
//indexjs
//获取应用实例
const app = getApp()
const util = require('//utils/utiljs')
const bletool = require('//utils/bletooljs')
Page({
data: {
// lists: [{ 'order_no': '1111', 'car_no': '321', 'car_type': '尚好捷', 'order_date': '2018-01-02 08:00', 'order_money': '1600', 'order_time': '4' }],
car_no: '',
order_no: '',
lists: [],
serviceId: '',//592B3370-3900-9A71-4535-35D4212D2837
serviceMac: '',//C9:9B:4C:E7:DE:10
service_psd: '',//855525B837253705595800000329
service_uuid: '',
deviceId:'',
characteristics:[] //特征值
},
onLoad: function (options) {
thisinitBle();
},
onReady: function () {
// 页面渲染完成
},
onShow: function () {
if (appglobalDatacar_nolength>0){
thisgetDeviceInfo();
}
},
onHide: function () {
// 页面隐藏
},
onUnload: function () {
// 页面关闭
appglobalDatacar_no=''
},
//蓝牙相关
//初始化蓝牙
initBle: function () {
var that = this;
wxonBluetoothAdapterStateChange(function (res) {
consolelog('adapterState changed, now is', res)
appglobalDatable_state = resavailable;
if (resavailable) {
thatinitBle();
} else {
utilshowToast('手机蓝牙已关闭');
appglobalDatable_isonnectting = false;
}
})
//打开蓝牙适配器
wxopenBluetoothAdapter({
success: function (res) {
consolelog('打开蓝牙适配器成功');
thatgetBluetoothAdapterState();
appglobalDatable_state = true;
thatonBluetoothDeviceFound();
},
fail: function (res) {
// fail
consolelog(res)
utilshowToast('请打开手机蓝牙');
},
complete: function (res) {
// complete
}
})
},
onBluetoothDeviceFound:function(){
var that = this;
//监听扫描
wxonBluetoothDeviceFound(function (res) {
// res电脑模拟器返回的为数组;手机返回的为蓝牙设备对象
consolelog('监听搜索新设备:', res);
thatupdateBleList([res])
})
},
getBluetoothAdapterState: function () {
var that = this;
wxgetBluetoothAdapterState({
success: function (res) {
var available = resavailable;
var discovering = resdiscovering;
if (!available) {
utilshowToast('蓝牙不可用');
} else {
if (!discovering) {
// thatstartBluetoothDevicesDiscovery();
}
}
}
})
},
startBluetoothDevicesDiscovery: function () {
var that = this;
var services = [];
servicespush(thisdataserviceId);
wxshowLoading({
title: '设备搜索中'
});
setTimeout(function () {
wxhideLoading();
if (appglobalDatadeviceIdlength==0){
utilshowModal('设备搜索失败,请重试');
}
}, 10000)
if(bletoolisIOS()){
wxstartBluetoothDevicesDiscovery({
services: services,
allowDuplicatesKey: true,
success: function (res) {
consolelog('ios搜索成功');
consolelog(res);
},
fail: function (err) {
consolelog(err);
}
});
}else{
wxstartBluetoothDevicesDiscovery({
// services: services,
allowDuplicatesKey: true,
success: function (res) {
consolelog('Android搜索成功');
consolelog(res);
},
fail: function (err) {
consolelog(err);
wxhideLoading();
thatstartBluetoothDevicesDiscovery();
// thatgetBluetoothAdapterState();
utilshowToast('搜索失败');
}
});
}
},
startConnectDevices: function (ltype, array) {
var that = this;
clearTimeout(thatgetConnectedTimer);
thatgetConnectedTimer = null;
wxstopBluetoothDevicesDiscovery({
success: function (res) {
// success
}
})
appglobalDatable_isonnectting = true;
consolelog('连接前:'+thatdeviceId);
wxcreateBLEConnection({
deviceId: thatdeviceId,
success: function (res) {
if (reserrCode == 0) {
consolelog('连接成功:');
thatgetService(thatdeviceId);
}
},
fail: function (err) {
consolelog('连接失败:', err);
wxhideLoading();
utilshowModal('设备连接失败,请重试');
// if (ltype == 'loop') {
// thatconnectDeviceIndex += 1;
// thatloopConnect(array);
// } else {
// thatstartBluetoothDevicesDiscovery();
// thatgetConnectedBluetoothDevices();
// }
appglobalDatable_isonnectting = false;
},
complete: function () {
}
});
},
getService: function (deviceId) {
var that = this;
// 监听蓝牙连接
wxonBLEConnectionStateChange(function (res) {
consolelog(res);
appglobalDatable_isonnectting = resconnected
if (!resconnected) {
utilshowToast('连接断开');
}
});
// 获取蓝牙设备service值
wxgetBLEDeviceServices({
deviceId: deviceId,
success: function (res) {
consolelog('获取蓝牙设备service值');
consolelog(res);
thatgetCharacter(deviceId, resservices);
}
})
},
getCharacter: function (deviceId, services) {
var that = this;
servicesforEach(function (value, index, array) {
if (valueisPrimary) {
thatsetData({
service_uuid: valueuuid,
deviceId: deviceId
})
appglobalDataservice_uuid= valueuuid;
appglobalDatadeviceId=deviceId;
}
});
//监听通知
wxonBLECharacteristicValueChange(function (res) {
// callback
consolelog('value change', res)
const hex = bletoolbuf2char(resvalue)
consolelog('返回的数据:', hex)
//配对密码
if (hexindexOf('855800000106') != -1) {
wxhideLoading();
var charact_write = thatdatacharacteristics[1]
bletoolwriteDataToDevice(thatdatadeviceId, thatdataservice_uuid, charact_write, thatdataservice_psd);
wxshowToast({
title: '设备已连接',
icon: 'success',
duration: 3000
})
setTimeout(function () {
bletoolwriteDataToDevice(thatdatadeviceId, thatdataservice_uuid, charact_write, '235525B837253705590400000273');
}, 2000)
} else if (hexindexOf('23040000') != -1) {
//启动成功
thatstarRenting();
}
})
wxgetBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: thatgetServiceUUID(),
success: function (res) {
wxgetBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: thatgetServiceUUID(),
success: function (res) {
consolelog('特征', res)
thatsetData({
characteristics:rescharacteristics
})
appglobalDatacharacteristics = rescharacteristics;
var charact_read = rescharacteristics[0]
},
loopConnect: function (devicesId) {
var that = this;
var listLen = devicesIdlength;
if (devicesId[thisconnectDeviceIndex]) {
thisdeviceId = devicesId[thisconnectDeviceIndex];
thisstartConnectDevices('loop', devicesId);
} else {
consolelog('已配对的设备小程序蓝牙连接失败');
thatstartBluetoothDevicesDiscovery();
thatgetConnectedBluetoothDevices();
}
},
//更新数据 devices为数组类型
updateBleList: function (devices) {
consolelog('设备数据:',devices);
var newData = thisdatableList
var that = this
var tempDevice = null;
for (var i = 0; i < deviceslength; i++) {
//ios设备
if (devices[i]devices != null) {
if (devices[i]deviceslength > 0) {
tempDevice = devices[i]devices[0];
}
else {
continue
}
}
//安卓
else {
tempDevice = devices[i];
}
if (!thisisExist(tempDevice)) {
newDatapush(tempDevice)
}
}
consolelog('数据:');
consolelog(newData)
thissetData({
bleList: newData
})
if (!appglobalDatable_isonnectting) {
var that = this;
thisdatableListforEach(function (value, index, array) {
//找到对应id的设备,ios判断服务id安卓判断mac地址
var deviceId = value['deviceId'];
if(bletoolisIOS()){
let advertisServiceUUID = value['advertisServiceUUIDs'][0];
if (advertisServiceUUID == thatdataserviceIdtoUpperCase()){
thatdeviceId = deviceId;
consolelog(thatdeviceId);
thatstartConnectDevices();
}
}else{
if (deviceId == thatdataserviceMac) {
thatdeviceId = deviceId;
consolelog(thatdeviceId);
thatstartConnectDevices();
}
}
});
}
},
//是否已存在 存在返回true 否则false
isExist: function (device) {
var tempData = thisdatableList
for (var i = 0; i < tempDatalength; i++) {
if (tempData[i]deviceId == devicedeviceId) {
return true
}
}
return false
},
//服务uuid
getServiceUUID: function () {
return bletoolstringTransition(thisdataservice_uuid);
},
getDeviceInfo: function () {
let car_no = appglobalDatacar_no;
var that = this;
wxrequest({
url: appglobalDataserverURL + 'c=car&a=getDeviceInfo&open_id=' + appglobalDataopen_id + '&car_no=' + car_no,
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: { 'content-type': 'application/json' }, // 设置请求的 header
success: function (res) {
// success
var data = resdata;
consolelog(data);
if (dataresult == 1) {
appglobalDataserviceId = datadataservice_id;
appglobalDataserviceMac = datadataservice_mac,
appglobalDataservice_psd = '85' + datadataservice_psd + '5800000329';
thatsetData({
serviceId: datadataservice_id,
serviceMac: datadataservice_mac,
service_psd: '85' + datadataservice_psd+'5800000329',
})
appstartBluetoothDevicesDiscovery();
// thatonBLECharacteristicValueChange();
} else {
utilshowModal(datamsg);
}
},
fail: function () {
},
complete: function () {
// complete
}
});
},
})
以上就是关于蓝牙技术是如何实现的需要什么语言去编程全部的内容,包括:蓝牙技术是如何实现的需要什么语言去编程、我电脑上自带的蓝牙怎么通过程序(c++,c#)来编写一个功能:检测与手机连接,超出范围 就断开、用keil c语言编写蓝牙串口通信遥控小车等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)