Bluetooth now correctly transmits the WANT-Vector and it gets displayed on the other device
This commit is contained in:
parent
9b53e1d3e3
commit
653c2068e7
@ -44,28 +44,61 @@ extension BluetoothPeripheral: CBPeripheralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addBTService() {
|
func addBTService() {
|
||||||
let myCharacteristic = CBMutableCharacteristic(type: BLE_CHARACTERISTIC_UUID_RX, properties: [.read, .write, .notify], value: nil, permissions: [.readable, .writeable])
|
let myCharacteristic = CBMutableCharacteristic(type: BLE_CHARACTERISTIC_UUID_RX, properties: [.read, .write, .notify, .writeWithoutResponse], value: nil, permissions: [.readable, .writeable])
|
||||||
let myService = CBMutableService(type: BLE_SERVICE_UUID, primary: true)
|
let myService = CBMutableService(type: BLE_SERVICE_UUID, primary: true)
|
||||||
myService.characteristics = [myCharacteristic]
|
myService.characteristics = [myCharacteristic]
|
||||||
peripheralManager!.add(myService)
|
peripheralManager!.add(myService)
|
||||||
|
}
|
||||||
|
|
||||||
|
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
|
||||||
peripheralManager!.startAdvertising([CBAdvertisementDataLocalNameKey : "RippleChat", CBAdvertisementDataServiceUUIDsKey: [BLE_SERVICE_UUID]])
|
peripheralManager!.startAdvertising([CBAdvertisementDataLocalNameKey : "RippleChat", CBAdvertisementDataServiceUUIDsKey: [BLE_SERVICE_UUID]])
|
||||||
print("Started Advertising")
|
print("Started Advertising")
|
||||||
|
if(peripheralManager?.delegate == nil) {
|
||||||
|
print("peripheral is nil")
|
||||||
|
} else {
|
||||||
|
print("peripheral is not nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func discoverServices(_ serviceUUIDs: [CBUUID]?) {
|
||||||
|
print("test Peripheral")
|
||||||
|
print("Discovering services... \(String(describing: serviceUUIDs))")
|
||||||
|
}
|
||||||
|
|
||||||
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||||
|
print("Discovering Services Peripheral")
|
||||||
|
// print("*******************************************************")
|
||||||
|
//
|
||||||
|
// if ((error) != nil) {
|
||||||
|
// print("Error discovering services: \(error!.localizedDescription)")
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// guard let services = peripheral.services else {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// //We need to discover the all characteristic
|
||||||
|
// for service in services {
|
||||||
|
// peripheral.discoverCharacteristics(nil, for: service)
|
||||||
|
// }
|
||||||
|
// print("Discovered Services: \(services)")
|
||||||
|
}
|
||||||
|
|
||||||
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
|
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
|
||||||
for request in requests {
|
for request in requests {
|
||||||
if let value = request.value {
|
if let value = request.value {
|
||||||
// Handle the received data
|
// Handle the received data
|
||||||
let receivedData = Data(value)
|
let receivedData = Data(value)
|
||||||
|
|
||||||
|
print(receivedData)
|
||||||
|
|
||||||
// Decode the received JSON string into your data structure
|
// Decode the received JSON string into your data structure
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
do {
|
do {
|
||||||
let receivedObject = try decoder.decode(String.self, from: receivedData)
|
let receivedObject = try decoder.decode(WantMessage.self, from: receivedData)
|
||||||
// Use the received object to update your app state as needed
|
// Use the received object to update your app state as needed
|
||||||
print("Received Write")
|
print("Received Write")
|
||||||
self.incomingMsg = receivedObject
|
self.incomingMsg = receivedObject.printMsg()
|
||||||
|
print(receivedObject.printMsg())
|
||||||
} catch {
|
} catch {
|
||||||
print("Failed to decode JSON: \(error)")
|
print("Failed to decode JSON: \(error)")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,23 +22,60 @@ class BluetoothController: NSObject, ObservableObject {
|
|||||||
super.init()
|
super.init()
|
||||||
self.centralManager = CBCentralManager(delegate: self, queue: .main)
|
self.centralManager = CBCentralManager(delegate: self, queue: .main)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension BluetoothController: CBCentralManagerDelegate {
|
extension BluetoothController: CBCentralManagerDelegate, CBPeripheralDelegate {
|
||||||
|
|
||||||
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
func centralManagerDidUpdateState(_ central: CBCentralManager) {
|
||||||
if central.state == .poweredOn {
|
if central.state == .poweredOn {
|
||||||
|
print("Device is powered on...")
|
||||||
self.centralManager?.scanForPeripherals(withServices: [BLE_SERVICE_UUID])
|
self.centralManager?.scanForPeripherals(withServices: [BLE_SERVICE_UUID])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
|
||||||
if !peripherals.contains(peripheral) {
|
if !peripherals.contains(peripheral) {
|
||||||
centralManager?.connect(peripheral)
|
peripheral.delegate = self
|
||||||
|
centralManager!.connect(peripheral)
|
||||||
self.peripherals.append(peripheral)
|
self.peripherals.append(peripheral)
|
||||||
self.peripheralNames.append(peripheral.name ?? "unnamed device")
|
self.peripheralNames.append(peripheral.name ?? "unnamed device")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
|
||||||
|
peripheral.discoverServices([BLE_SERVICE_UUID])
|
||||||
|
print("Connected to device \(String(describing: peripheral.name))")
|
||||||
|
if(centralManager?.delegate == nil) {
|
||||||
|
print("central is nil")
|
||||||
|
} else {
|
||||||
|
print("central is not nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||||
|
// print("Discovering services...")
|
||||||
|
// peripheral.discoverCharacteristics(BLE_CHARACTERISTIC_UUID_RX)
|
||||||
|
// }
|
||||||
|
|
||||||
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
|
||||||
|
print("*******************************************************")
|
||||||
|
|
||||||
|
if ((error) != nil) {
|
||||||
|
print("Error discovering services: \(error!.localizedDescription)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard let services = peripheral.services else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//We need to discover the all characteristic
|
||||||
|
for service in services {
|
||||||
|
peripheral.discoverCharacteristics(nil, for: service)
|
||||||
|
}
|
||||||
|
print("Discovered Services: \(services)")
|
||||||
|
}
|
||||||
|
|
||||||
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
|
||||||
guard let characteristics = service.characteristics else {
|
guard let characteristics = service.characteristics else {
|
||||||
@ -47,12 +84,19 @@ extension BluetoothController: CBCentralManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for characteristic in characteristics {
|
for characteristic in characteristics {
|
||||||
if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) {
|
if characteristic.uuid.isEqual(BLE_CHARACTERISTIC_UUID_RX) {
|
||||||
// This characteristic supports writing
|
self.writeCharacteristics.append(characteristic)
|
||||||
writeCharacteristics.append(characteristic)
|
peripheral.setNotifyValue(true, for: characteristic)
|
||||||
|
peripheral.readValue(for: characteristic)
|
||||||
|
print("Characteristic: \(characteristic.uuid)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||||
|
print("updating characteristic value...")
|
||||||
|
print(characteristic.value ?? "Characteristic is nil")
|
||||||
|
}
|
||||||
|
|
||||||
func writeToCharacteristics(message: String) {
|
func writeToCharacteristics(message: String) {
|
||||||
guard let messageData = message.data(using: .utf8) else {
|
guard let messageData = message.data(using: .utf8) else {
|
||||||
|
|||||||
@ -24,8 +24,8 @@ struct PeeringView: View {
|
|||||||
do {
|
do {
|
||||||
let WANT_msg = WantMessage(friends: dataStore.friends)
|
let WANT_msg = WantMessage(friends: dataStore.friends)
|
||||||
let encoded_msg = try JSONEncoder().encode(WANT_msg)
|
let encoded_msg = try JSONEncoder().encode(WANT_msg)
|
||||||
//btController.writeToCharacteristics(message: String(data: encoded_msg, encoding: .utf8)!)
|
btController.writeToCharacteristics(message: String(data: encoded_msg, encoding: .utf8)!)
|
||||||
btController.writeToCharacteristics(message: "Test")
|
//btController.writeToCharacteristics(message: "Test")
|
||||||
print("Pressed Button")
|
print("Pressed Button")
|
||||||
} catch {
|
} catch {
|
||||||
fatalError(error.localizedDescription)
|
fatalError(error.localizedDescription)
|
||||||
@ -47,7 +47,10 @@ struct PeeringView_Previews: PreviewProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct WantMessage: Codable {
|
struct WantMessage: Codable {
|
||||||
var commmand = "WANT"
|
var command = "WANT"
|
||||||
var friends = [String:Int]()
|
var friends = [String:Int]()
|
||||||
|
|
||||||
|
func printMsg() -> String {
|
||||||
|
return ("\(command) : \(friends.description)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user