Finding generic Bluetooth devices within reach

后端 未结 4 1919
半阙折子戏
半阙折子戏 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:40

    The GKSession class which is part of the GameKit framework is what you are looking for as it provides the ability to discover and connect to nearby iOS devices using Bluetooth.

    0 讨论(0)
  • 2021-01-03 07:49

    I've been messing with the private framework for a few days, and getting a list of nearby devices is pretty straightforward.

    First, you have to enable device scanning using:

    [[BluetoothManager sharedInstance] setDeviceScanningEnabled:YES];
    

    If there are devices within range it will start posting BluetoothDeviceDiscoveredNotification notifications to the notification center. Subscribe to these and the object in the NSNotification delivered to the callback will be of type BluetoothDevice*.

    I'm sure the BluetoothManager stores any discovered devices somewhere, but I just threw everything into my own NSMutableArray.

    Unfortunately I still haven't figured out how to actually pair with a device using the private API.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-03 07:59

    You need to use BluetoothManager to turn Bluetooth on, and then to enable bluetooth scanning of remote devices.

    Having registered a notification callback, you will get the discovered devices. The notification object is actually a pointer to a BluetoothDevice object.

    Get the BluetoothDevice object pointer, and from there you can get the name, address or connect to the remote device.

    There is no RSSI here, you can see the complete list of methods by looking at the BluetoothDevice.h file.

    I wrote a complete sample, compatible with iOS 5.1, here: http://www.pocketmagic.net/?p=2827

    Good luck!

    0 讨论(0)
提交回复
热议问题