Finding generic Bluetooth devices within reach

后端 未结 4 1952
半阙折子戏
半阙折子戏 2021-01-03 07:03

We are using the iOS private framework BluetoothManager for a simple experiment -- to find discoverable generic (non-iOS) BT devices within reach. Now, only the fol

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 07:51

    Rather than using the private BluetoothManager framework, why not use the publicly available Game Kit framework which exposes several methods to support detecting & connecting two devices via Bluetooth and exchanging data between them (and it's not restricted to just being used in games despite its name)

    There's a pretty comprehensive tutorial here.

    You can either use the built-in GKPeerPickerController, or manage it yourself with the following

    // create a session on each of your devices
    GKSession *session = [[GKSession alloc] initWithSessionID:@"uniqueSessionID" displayName:@"deviceDisplayName" sessionMode:GKSessionModePeer
    // set the delegate on the session
    session.delegate = self;
    

    make sure to use the same session id across all devices, but make the name unique to the device

    then implement the following delegate method

    - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
    

    and when new peers advertise themselves with the same session id, this will be called and their state should be

    GKPeerStateAvailable
    

提交回复
热议问题