android如何获得已配对设备的蓝牙mac地址

android如何获得已配对设备的蓝牙mac地址,第1张

送上我迟来的答案,不知你这条帮助是哪年留下的,我是通过手机看到你的求助,但是却看不到你是那年留下的问题,能看到下面有人在15年的答复,估计应该是5年之前的问题了,即便能帮助到你也是作用不大了。我这有一款APP可以看到你手机上曾经连接过的所有蓝牙设备的MAC地址,只要是你手机曾连接过的设备,就全部都能看到Mac地址,APP名为

Walkie-Talkie via Bluetooth

只可惜我看到你的求助太迟了,已相隔5年之久,若当时看到多好啊一定会帮得到你^_^

openBluetoothAdapter() {

uniopenBluetoothAdapter({

success: e => {

// consolelog('初始化蓝牙成功:' + eerrMsg);

thisonBluetoothDeviceFound()

unigetBluetoothAdapterState({

success: function(res) {

// 初始化完毕开始搜索

if (resavailable) {

if (resdiscovering) {

// consolelog('停止连接');

thiscloseBluetooth()

}

unistartBluetoothDevicesDiscovery({

success: res => {

},

fail: res => {

onsolelog("查找设备失败!");

unishowToast({

icon: "none",

title: "查找设备失败!",

duration: 3000,

})

}

})

} else {

consolelog('本机蓝牙不可用')

}

}

})

},

fail: e => {

if (eerrCode == 10001) {

unishowModal({

title: "提示",

content: "您的蓝牙没有打开,请打开蓝牙",

success: function(res) {

if (resconfirm == true) {

if (unigetSystemInfoSync()platform == 'ios') {

} else if (unigetSystemInfoSync()platform === 'android') {

}

} else {

uninavigateBack({

url: '//detail3/detail3'

})

}

},

});

}

consolelog('初始化蓝牙失败,错误码:' + (eerrCode || eerrMsg));

}

});

},

onBluetoothDeviceFound() {

// consolelog("监听寻找新设备");

unionBluetoothDeviceFound(devices => {

// consolelog('开始监听寻找到新设备的事件');

// consolelog(deviceslength);

thisgetBluetoothDevices();

});

},

getBluetoothDevices() {

// consolelog("获取蓝牙设备");

unigetBluetoothDevices({

success: res => {

// consolelog('获取蓝牙设备');

thisresdic = res

// consolelog(res)

// consolelog('deviceslength===='+thisresdicdeviceslength);

for (let i = 0; i < thisresdicdeviceslength; i++) {

let devicesDic = thisresdicdevices[i];

if (devicesDicname && devicesDicname == '<Dobiy>' && devicesDicadvertisServiceUUIDs) {

thisdeviceIdStr = devicesDicdeviceId

thisstopBluetooth()

thiscreateBLEConnection(devicesDicdeviceId)

}

}

}

});

},

createBLEConnection(deviceIds) {

consolelog("连接蓝牙---------------" + deviceIds)

// setTimeout(function(){

//    thatcloseBLEConnection();

//    thatcreateBLEConnection(deviceIds)

// },5000)

unicreateBLEConnection({

deviceId: deviceIdstoString(),

success: (res) => {

consolelog('蓝牙连接成功');

// thisgetBLEDeviceServices(deviceIds)

thisstopBluetooth()

var that = this

unigetSystemInfo({

success(res) {

if (resplatform == 'ios') {

setTimeout(() => {

thatgetBLEDeviceServices(deviceIds)

}, 2000)

} else {

setTimeout(() => {

thatnogetBLEDeviceServices(deviceIds)

}, 3000)

}

},

fail(res) {

consolelog(777777777777777)

}

})

},

fail: (res) => {

consolelog('蓝牙连接失败:' + reserrCode)

thatcloseBLEConnection();

thatcreateBLEConnection(deviceIds)

},

});

},

async getBLEDeviceServices(deviceIds) {

var that = this

let deviceServices = await unigetBLEDeviceServices({

deviceId: deviceIds

})

if (deviceServices[0] == null) {

let services = deviceServices[1]services

if (serviceslength == 0) {

setTimeout(() => {

thatgetBLEDeviceServices(deviceIds)

}, 1500)

} else {

consolelog(services, 88888888888)

for (let i = 0; i < serviceslength; i++) {

let serviceId = services[i]uuid

// consolelog('serviceId====' + serviceId);

let characteristicsDic = await unigetBLEDeviceCharacteristics({

deviceId: deviceIds,

serviceId: serviceId,

})

then(data => {

// consolelog(JSONstringify(data))

if (data[0] == null) {

let characteristicsArr = data[1]characteristics

let properties = characteristicsArr[0]properties

consolelog(properties)

if (propertieswrite == true) {

thatserviceIdStr = serviceId;

thatcharacteristicsIdStr = data[1]characteristics[0]uuid;

thatnotifyBLECharacteristicValueChange(thatcharacteristicsIdStr)

// consolelog(deviceIds)

// consolelog(thatserviceIdStr)

// consolelog(thatcharacteristicsIdStr)

thatwriteBLECharacteristic(deviceIds, serviceId, thatcharacteristicsIdStr)

} else {

consolelog('不可编辑', data[1]characteristics[0]uuid)

}

}

})

}

}

} else {

unishowModal({

title: "提示",

content: "获取设备服务信息失败,请重新获取",

showCancel: false,

success(res) {

uninavigateBack({

url: '/uploadImg/uploadImg'

})

}

})

}

},

nogetBLEDeviceServices(deviceIds) {

var that = this

consolelog(777777)

unigetBLEDeviceServices({

deviceId: deviceIds,

success(res) {

let services = resservices

consolelog(services, 55555)

if (serviceslength == 0) {

setTimeout(() => {

thatnogetBLEDeviceServices(deviceIds)

}, 3000)

} else {

for (let i = 0; i < serviceslength; i++) {

let serviceId = services[i]uuid

//这个比对serviceId,设备厂家会告诉,因为安卓获取特征值都是不可写入的,iOS 的可以写入的,所以安卓直接判断serviceId是不是厂家发的serviceId

if (serviceId == '') {

unigetBLEDeviceCharacteristics({

deviceId: deviceIds,

serviceId: serviceId,

success(result) {

let propertiesDic = resultcharacteristics[0]

consolelog(propertiesDic, 55555)

// if (propertiesDicpropertieswrite == true) {

thatserviceIdStr = serviceId;

thatcharacteristicsIdStr = propertiesDicuuid;

thatnotifyBLECharacteristicValueChange(thatcharacteristicsIdStr)

setTimeout(() =>{

thatwriteBLECharacteristic(deviceIds, serviceId, thatcharacteristicsIdStr)

}, 2000)

// } else {

// consolelog('不可编辑', propertiesDicuuid)

// }

},

fail(result) {

consolelog(result, 2222222)

}

})

}

}

}

},

fail(res) {

consolelog(res, 8888888)

}

})

},

一定要调用这个方法,否则无法监听设备返回的数据,之前一直以为在uniwriteBLECharacteristicValue的回调里能得到返回的值,这个方法没有用,结果试了几天都不行,

notifyBLECharacteristicValueChange(characteristicId) {

consolelog(characteristicId, 'characteristicId')

uninotifyBLECharacteristicValueChange({

state: true, // 启用 notify 功能

deviceId: thisdeviceIdStr,

serviceId: thisserviceIdStr,

characteristicId: thischaracteristicsIdStr,

success: (res) => {

// consolelog(res)

// consolelog(thischaracteristicId)

consolelog('notifyBLECharacteristicValueChange success', res)

// thatwriteBLECharacteristic(deviceIds, serviceId, thatcharacteristicsIdStr)             

thisonBLECharacteristicValue()

},

fail: (res) => {

consolelog('notifyBLECharacteristicValueChange success2', reserrMsg)

}

})

},

onBLECharacteristicValue() {

var that = this

unionBLECharacteristicValueChange(function(res) {

// consolelog(`characteristic ${rescharacteristicId} has changed, now is ${resvalue}`)

let resultStr = thatbufferString(resvalue)

// consolelog(resultStr,111)

if (resultStr && resultStr != '') {

//resultStr就是设备上返回的数据,根据设备不同,获取的设备格式也就不同,自己筛选吧

}

})

},

writeBLECharacteristic(deviceId, serviceId, characteristicId) {

var that = this

consolelog(1111111111)

let sgInt = thatstring2buffer('<sAg>')

// consolelog(sgIntbyteLength)

uniwriteBLECharacteristicValue({

deviceId: thatdeviceIdStr,

serviceId: thatserviceIdStr,

characteristicId: thatcharacteristicsIdStr,

value: sgInt,

success: function(res) {

consolelog(typeof(res))

consolelog('writeBLECharacteristicValue success', reserrMsg)

// thatcloseBluetooth()

},

fail: function(res) {

consolelog(typeof(res))

consolelog('writeBLECharacteristicValue fail==', reserrCode, reserrMsg)

}

})

},

//字符串转arraybuffer

string2buffer: function(str) {

// 首先将字符串转为16进制

let val = ""

for (let i = 0; i < strlength; i++) {

if (val === '') {

val = strcharCodeAt(i)toString(16)

} else {

val += ',' + strcharCodeAt(i)toString(16)

}

}

// consolelog(val)

// 将16进制转化为ArrayBuffer

return new Uint8Array(valmatch(/[\da-f]{2}/gi)map(function(h) {

return parseInt(h, 16)

}))buffer

},

//arraybuffer 转字符串

bufferString: function(str) {

// ArrayBuffer转16进度字符串示例

function ab2hex(buffer) {

const hexArr = Arrayprototypemapcall(

new Uint8Array(buffer),

function(bit) {

return ('00' + bittoString(16))slice(-2)

}

)

return hexArrjoin('')

}

//16进制

let systemStr = ab2hex(str)

// consolelog(hexCharCodeToStr(systemStr),99)

function hexCharCodeToStr(hexCharCodeStr) {

var trimedStr = hexCharCodeStrtrim();

var rawStr =

trimedStrsubstr(0, 2)toLowerCase() === "0x"

trimedStrsubstr(2) :

trimedStr;

var len = rawStrlength;

if (len % 2 !== 0) {

alert("Illegal Format ASCII Code!");

return "";

}

var curCharCode;

var resultStr = [];

for (var i = 0; i < len; i = i + 2) {

curCharCode = parseInt(rawStrsubstr(i, 2), 16); // ASCII Code Value

let str5 = StringfromCharCode(curCharCode)

if (str5startsWith('\n') == false) {

resultStrpush(StringfromCharCode(curCharCode));

}

}

return resultStrjoin("");

}

// consolelog(hexCharCodeToStr(systemStr),888)

return hexCharCodeToStr(systemStr)

},

//监听低功耗蓝牙连接状态的改变事件

onBLEConnectionStateChange() {

unionBLEConnectionStateChange(function(res) {

if (!resconnected) {

// consolelog('蓝牙断开链接')

thiscloseBluetooth()

}

})

},

stopBluetooth() {

unistopBluetoothDevicesDiscovery({

success: e => {

// consolelog('停止搜索蓝牙设备:' + eerrMsg);

},

fail: e => {

// consolelog('停止搜索蓝牙设备失败,错误码:' + eerrCode);

}

});

},

//关闭蓝牙模块

closeBluetooth() {

unicloseBluetoothAdapter({

success(res) {

consolelog(res)

}

})

}

},

打开手机的“设置”,再找到“蓝牙”并打开。

点击手机的“搜索蓝牙设备”。

配对成功后,可在“已配对的设备”中看到

手机蓝牙:

蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,利用"蓝牙"技术,能够有效地简化掌上电脑、笔记本电脑和移动电话手机等移动通信终端设备之间的通信,也能够成功地简化以上这些设备与因特网Internet之间的通信,从而使这些现代通信设备与因特网之间的数据传输变得更加迅速高效,为无线通信拓宽道路。蓝牙采用分散式网络结构以及快跳频和短包技术,支持点对点及点对多点通信,工作在全球通用的24GHz ISM(即工业、科学、医学)频段。其数据速率为1Mbps。采用时分双工传输方案实现全双工传输。

以上就是关于android如何获得已配对设备的蓝牙mac地址全部的内容,包括:android如何获得已配对设备的蓝牙mac地址、uniapp 通过蓝牙连接设备并发送命令、如何查看安卓手机中已配对蓝牙设备信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9712068.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存