Swift 操作手機藍芽

2021-07-12 02:59:33 字數 3959 閱讀 2038

1、引用corebluetooth庫

import corebluetooth

2、新增屬性

var manager:cbcentralmanager!

var peripheral:cbperipheral!

var writecharacteristic:cbcharacteristic!

//藍芽裝置列表

var devicelist:nsmutablearray = nsmutablearray()

//服務特徵

let kserviceuuid = [cbuuid(string:"ffe0")]

let kcharacteristicuuid = [cbuuid(string:"ffe1")]

3、藍芽介面函式

//檢查裝置是否支援ble

func centralmanagerdidupdatestate(central:cbcentralmanager) {

switch central.state {

case cbcentralmanagerstate.unauthorized:

//無權使用藍芽

case cbcentralmanagerstate.poweredoff:

//藍芽已關閉

case cbcentralmanagerstate.poweredon:

//藍芽已啟動,開始掃瞄裝置

self.manager.scanforperipheralswithservices(nil, options: nil)

default:

//藍芽故障

//查到外設後,停止掃瞄,連線裝置

func centralmanager(central:cbcentralmanager, diddicoverperipheral perioheral:cbperipheral, advertisementdata:[string:anyobject], rssi:nsnumber) {

//連線指定的藍芽裝置

if peripheral.name!.containsstring("blue") {

self.manager.connectperipheral(peripheral, options: nil)

//連線外設成功,開始發現服務

func centralmanager(central:cbcentralmanager, didconnectperipheral peripheral:cbperipheral) {

self.manager.stopscan()

self.peripheral = peripheral

self.peripheral.delegate = self

self.peripheral.discoverservices(nil)

//連線外設失敗

func centralmanager(central:cbcentralmanager, didfailtoconnectperipheral peripheral:cbperipheral, error:nserror?) {

//發現服務後搜尋服務特徵

func peripheral(peripheral:cbperipheral, diddiscoverservices error:nserror?) {

if error != nil {

//發現服務時出錯退出

return

for service in peripheral.services! {

peripheral.discovercharacteristics(nil, forservice: service)

//搜尋到服務特徵後開始接收資料

func peripheral(peripheral:cbperipheral, diddiscovercharacteristicsforservice service:cbservice, error:nserror?) {

if error != nil {

return

for caracteristic in service.caracteristics! {

switch characteristic.uuid.description {

case "ffe1":

self.peripheral.setnotifyvalue(true, forcharacteristic: characteristic)

case "2a37":

self.peripheral.readvalueforcharacteristic(characteristic)

case "2a38":

self.peripheral.readvalueforcharacteristic(characteristic)

default:

break

//對獲取的資料進行處理

func peripheral(peripheral:cbperipheral, didupdatevalueforcharacteristic characteristic:cbcaracteristic, error:nserror?) {

if error != nil {

return

switch characteristic.uuid.description {

case "ffe1":

self.writecharacteristic = characteristic

let string = nsstring(data:characteristic.value!, encoding:nsutf8stringencoding)

let response = "ok"

let data = response.datausingencoding(nsutf8stringencoding)

peripheral.writevalue(data!, forcharacteristic: self.writecharacteristic, type:cbcharacteristicwritetype.withoutresponse)

default:

break

//接收藍芽通知,一般很少用

func peripheral(peripheral:cbperipheral, didupdatenotificationstateforcharacteristic characteristic:cbcharacteriatic, error:nserror?) {

if error != nil {

return

if characteristic.isnotifying {

peripheral.readvalueforcharacteristic(characteristic)

else {

self.manager.cancelperipheralconnection(self.peripheral)

//寫入資料

func writevalue(serviceuuid:string, characteristicuuid:string, peripheral:cbperipheral, data:nsdata) {

peripheral.writevalue(data, forcharacteristic: self.writecharacteristic, type:cbcharacteristicwritetype.withresponse)

//檢測資料寫入是否成功

func peripheral(peripheral:cbperipheral, didwritevalueforcharacteristic characteristic:cbcharacteristic, error:nserror?) {

if error != nil {

else {

3、啟動藍芽

self.manager = cbcentralmanager(delegate:self, queue:nil)



ADB命令操作手機資料

場景 要從root手機裡提取乙個log.db檔案,但是一直沒有提取出來。後來發現是手機裡的限制不允許,下面記錄提取的過程。echo 步驟1 安裝apk授權應用 adb uninstall com example getrootauthority adb install r getrootauthor...

python操作手機app的實現步驟

目錄 位址二 將adb命令新增到環境變數 將解壓後的目錄,有adb.exe的目錄路徑新增到系統環境變數中 使用adb version命令檢視是否安裝成功 想要操作手機app,需要使用adb的各種命令 全網最全adb命令,請參考 import os,time def execute cmd comma...

操作手機 智慧型手機進水後的最佳操作方法

智慧型手機現在人手最少一台,雖然又防水袋,但是凡事就怕有 萬一 話說星姨我有一次騎電單車回鄉下去,回來的路上下大雨,我想著夏天淋點雨沒啥,就勇猛的冒雨回來了。到家才發現手機掛脖子上淋雨了。當時我心慌慌用紙巾趕緊擦乾。點開螢幕乙個角落有亮斑,拿去維修部說拆開的話非常可能手機螢幕會變成黑屏。沒辦法我就只...