Anyone tried with Core Bluetooth?

Havr anyone tried using Core Bluetooth with the ObjC api? Havent figured out how to do it yet. Any example would be appreciated :slight_smile:

Can you get a list of methods in a objc class? I tried this, but the scan method is missing…


CBD = objc.delegate("CBCentralManagerDelegate")

function CBD:centralManagerDidUpdateState(central)
    print(central.state)
end
    
function setup()
    d = CBD()
    cm = objc.cls.CBCentralManager(d, nil)
    cm:scanForPeripherals_withServices_(nil)
end

Ok, I made it so far. It wasn’t that obvious how to figure out the naming. Does it say in the documentation that the arguments must be named objClassName to work correctly. But fun to get it to discover BT-devices so far :slight_smile:

CBD = objc.delegate("CBCentralManagerDelegate")

function CBD:centralManagerDidUpdateState_(central)
    print("did", central, central.state)
    print(cm.state)
    if cm.state == 5 then -- poweredOn
        print("scan")
        cm:scanForPeripheralsWithServices_options_(nil)
    end
end

function CBD:centralManager_didDiscoverPeripheral_advertisementData_RSSI_(central,objCBPeripheral,c,d)
    print("discovered name", objCBPeripheral.name)
end

function setup()
    s = Scene()
    d = CBD()
    CM = objc.cls.CBCentralManager
    cm = CM()
    cm:initWithDelegate_queue_(d,nil)
end

I managed to get it working now. I had to convert the NSData to base64 to be able to read it.

Now it reads the heartrate, steps, accelerometer from my watch and displays it. It also sends a message to it.

Should I be able to link this to my own watch?

I guess you have to figure out the api for your watch. I think the pinetime watch uses a standard api for heartrate, but I havent tested it with any other device.

I’ve now rewritten it to read the step counter and motion sensor as well. And send a message to the watch.