iOS CoreBluetooth接收数据

时间:2015-10-12 09:49:24

标签: ios swift2 core-bluetooth

我正在开展一个项目,我通过BLE与微控制器进行通信。我可以使用以下代码成功发送数据:

class Languages(models.Model):
    name = models.CharField(unique=True, max_length=255)
    short_name = models.CharField(unique=True, max_length=4, blank=True, null=True)
    active = models.BooleanField(default="")

    class Meta:
        managed = False
        db_table = 'languages'
        ordering = ('id',)

class GenresVideo(models.Model):
    genre_id = models.IntegerField()
    language = models.ForeignKey('Languages')
    name = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'genres_video'
        unique_together = (('genre_id', 'language'),)
        ordering = ('genre_id',)

class MoviesGenres(models.Model):
    movie = models.ForeignKey(Movies)
    genre_id = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'movies_genres'
        unique_together = (('movie', 'genre_id'),)

但我无法找到有关如何接收数据的任何信息。我想在UILabel中显示收到的数据。有人知道如何设置一个函数,当有数据发送到iPhone时调用该函数吗?

1 个答案:

答案 0 :(得分:0)

如果你能写,那么阅读数据就容易多了。 首先,您需要扫描要从中接收数据的服务。像

peripheral.discoverServices(nil)

当找到服务后,将调用委托方法。然后您需要发现要从中读取数据的特征

func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
    self.statusLabel.text = "Looking at peripheral services"
    for service in peripheral.services {
        let thisService = service as CBService
        if service.UUID == yourseviceid {
            // Discover characteristics of  Service
            peripheral.discoverCharacteristics(nil, forService: thisService)
        }
        // Uncomment to print list of UUIDs
        //println(thisService.UUID)
    }
}

当给定服务的中心发现特征时,CBPeripheralDelegate协议的didDiscoverCharacteristicsForService方法被调用

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {



    // check the uuid of each characteristic to find config and data characteristics of the one you are interested 
    for charateristic in service.characteristics {
        let thisCharacteristic = charateristic as CBCharacteristic
        // check for data characteristic
        if thisCharacteristic.UUID == yourcharUUID {
            // Enable Notification to if the if it will be updated periodically and you want to update 
            peripheralDevice.setNotifyValue(true, forCharacteristic: thisCharacteristic)
//Or just read the value if you just want to read
peripheralDevice.readValueForCharacteristic(thisCharacteristic)
        }

    }

}

然后您的值将在以下委托方法中恢复。

// Get data values when they are updated
func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {



    if characteristic.UUID == yourUUID {
        // Convert the revived NSData to array of signed 16 bit values
        let dataBytes = characteristic.value

        //parse the data received and display where you want 



    }
}