【问题标题】:9dof razor and BLE mini9dof 剃须刀和 BLE mini
【发布时间】:2014-07-16 18:37:39
【问题描述】:

我正在尝试通过 BLE mini(Red Bear 实验室)将数据从 9dof Razor IMU 模块(来自 Sparkfun)传输到 BLEChat_Central_OSX 应用程序,但它不起作用:/

我使用的代码是:

我尝试通过 BLE 仅使用以下代码传输一个值:

     Serial.print(int(smoothedAX));

     Serial.println();

但在 Xcode 上运行 BLEChat 时,我只收到“i”和“Y”值:

2014-07-16 18:27:01.389 BLEChat Central[395:303] scanForPeripheralsWithServices

2014-07-16 18:27:02.108 BLEChat Central[395:303] didDiscoverPeripheral

2014-07-16 18:27:02.426 BLEChat Central[395:303] Duplicate UUID found updating...

2014-07-16 18:27:03.389 BLEChat Central[395:303] Stopped Scanning

2014-07-16 18:27:03.389 BLEChat Central[395:303] Known peripherals : 1

2014-07-16 18:27:03.389 BLEChat Central[395:303] List of currently known peripherals :

2014-07-16 18:27:03.390 BLEChat Central[395:303] 0  |  308B4C0E-F756-434C-84D4-B474B1C12AD6

2014-07-16 18:27:03.390 BLEChat Central[395:303]
------------------------------------

2014-07-16 18:27:03.390 BLEChat Central[395:303] Peripheral Info :

2014-07-16 18:27:03.390 BLEChat Central[395:303] UUID : 308B4C0E-F756-434C-84D4-B474B1C12AD6

2014-07-16 18:27:03.390 BLEChat Central[395:303] Name : Biscuit

2014-07-16 18:27:03.391 BLEChat Central[395:303]
-------------------------------------

2014-07-16 18:27:03.391 BLEChat Central[395:303] Connecting to peripheral with UUID : 308B4C0E-F756-434C-84D4-B474B1C12AD6

2014-07-16 18:27:03.567 BLEChat Central[395:303] Connected to 308B4C0E-F756-434C-84D4-B474B1C12AD6 successful

2014-07-16 18:27:03.582 BLEChat Central[395:303] ->Connected

2014-07-16 18:27:03.747 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.747 BLEChat Central[395:303] y

2014-07-16 18:27:03.749 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.749 BLEChat Central[395:303] y

2014-07-16 18:27:03.750 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.750 BLEChat Central[395:303] i

2014-07-16 18:27:03.814 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.814 BLEChat Central[395:303] i

2014-07-16 18:27:03.816 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.816 BLEChat Central[395:303] i

2014-07-16 18:27:03.817 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.817 BLEChat Central[395:303] i

2014-07-16 18:27:03.818 BLEChat Central[395:303] Length: 2

2014-07-16 18:27:03.818 BLEChat Central[395:303] i

我还尝试通过改编 arduino 代码来传输数据:

     // uint16_t value = smoothedAX;

     uint16_t value = 123; // fake reading

     BLEMini_write(0x0B);

     BLEMini_write(value >> 8);

     BLEMini_write(value);

但随后我立即收到一条错误消息:

2014-07-16 18:28:04.930 BLEChat Central[425:303] scanForPeripheralsWithServices

2014-07-16 18:28:04.964 BLEChat Central[425:303] didDiscoverPeripheral

2014-07-16 18:28:04.964 BLEChat Central[425:303] Duplicate UUID found updating...

2014-07-16 18:28:06.932 BLEChat Central[425:303] Stopped Scanning

2014-07-16 18:28:06.932 BLEChat Central[425:303] Known peripherals : 1

2014-07-16 18:28:06.932 BLEChat Central[425:303] List of currently known peripherals :

2014-07-16 18:28:06.932 BLEChat Central[425:303] 0  |  308B4C0E-F756-434C-84D4-B474B1C12AD6

2014-07-16 18:28:06.932 BLEChat Central[425:303] ------------------------------------

2014-07-16 18:28:06.933 BLEChat Central[425:303] Peripheral Info :

2014-07-16 18:28:06.933 BLEChat Central[425:303] UUID : 308B4C0E-F756-434C-84D4-B474B1C12AD6

2014-07-16 18:28:06.933 BLEChat Central[425:303] Name : Biscuit

2014-07-16 18:28:06.933 BLEChat Central[425:303] -------------------------------------

2014-07-16 18:28:06.933 BLEChat Central[425:303] Connecting to peripheral with UUID : 308B4C0E-F756-434C-84D4-B474B1C12AD6

2014-07-16 18:28:06.975 BLEChat Central[425:303] Connected to 308B4C0E-F756-434C-84D4-B474B1C12AD6 successful

2014-07-16 18:28:06.991 BLEChat Central[425:303] ->Connected

2014-07-16 18:28:07.131 BLEChat Central[425:303] Length: 2

2014-07-16 18:28:07.132 BLEChat Central[425:303] (null)

2014-07-16 18:28:07.133 BLEChat Central[425:303] An uncaught exception was raised

2014-07-16 18:28:07.134 BLEChat Central[425:303] -[__NSCFString appendString:]: nil argument

2014-07-16 18:28:07.134 BLEChat Central[425:303] (

0   CoreFoundation                      0x00007fff82e5725c __exceptionPreprocess + 172

1   libobjc.A.dylib                     0x00007fff88243e75 objc_exception_throw + 43

2   CoreFoundation                      0x00007fff82e5710c +[NSException raise:format:] + 204

3   CoreFoundation                      0x00007fff82e26def mutateError + 159

4   BLEChat Central                     0x00000001000054d1 -[RBLAppDelegate bleDidReceiveData:length:] + 257

5   BLEChat Central                     0x0000000100004cb2 -[BLE peripheral:didUpdateValueForCharacteristic:error:] + 770

6   CoreBluetooth                       0x00007fff884b4089 -[CBCentralManager xpcConnection:didReceiveMsg:args:] + 101

7   CoreBluetooth                       0x00007fff884bd88b __34-[CBXpcConnection handleMsg:args:]_block_invoke + 81

8   libdispatch.dylib                   0x00007fff884c81bb _dispatch_call_block_and_release + 12

9   libdispatch.dylib                   0x00007fff884c528d _dispatch_client_callout + 8

10  libdispatch.dylib                   0x00007fff884ccef0 _dispatch_main_queue_callback_4CF + 333

11  CoreFoundation                      0x00007fff82dbe4f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9

12  CoreFoundation                      0x00007fff82d79714 __CFRunLoopRun + 1636

13  CoreFoundation                      0x00007fff82d78e75 CFRunLoopRunSpecific + 309

14  HIToolbox                           0x00007fff877cea0d RunCurrentEventLoopInMode + 226

15  HIToolbox                           0x00007fff877ce7b7 ReceiveNextEventCommon + 479

16  HIToolbox                           0x00007fff877ce5bc _BlockUntilNextEventMatchingListInModeWithFilter + 65

17  AppKit                              0x00007fff833f624e _DPSNextEvent + 1434

18  AppKit                              0x00007fff833f589b -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122

19  AppKit                              0x00007fff833e999c -[NSApplication run] + 553

20  AppKit                              0x00007fff833d4783 NSApplicationMain + 940

21  BLEChat Central                     0x0000000100005122 main + 34

22  libdyld.dylib                       0x00007fff87eea5fd start + 1

)

2014-07-16 18:28:07.135 BLEChat Central[425:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'

*** First throw call stack:

(

0   CoreFoundation                      0x00007fff82e5725c __exceptionPreprocess + 172

1   libobjc.A.dylib                     0x00007fff88243e75 objc_exception_throw + 43

2   CoreFoundation                      0x00007fff82e5710c +[NSException raise:format:] + 204

3   CoreFoundation                      0x00007fff82e26def mutateError + 159

4   BLEChat Central                     0x00000001000054d1 -[RBLAppDelegate bleDidReceiveData:length:] + 257

5   BLEChat Central                     0x0000000100004cb2 -[BLE peripheral:didUpdateValueForCharacteristic:error:] + 770

6   CoreBluetooth                       0x00007fff884b4089 -[CBCentralManager xpcConnection:didReceiveMsg:args:] + 101

7   CoreBluetooth                       0x00007fff884bd88b __34-[CBXpcConnection handleMsg:args:]_block_invoke + 81

8   libdispatch.dylib                   0x00007fff884c81bb _dispatch_call_block_and_release + 12

9   libdispatch.dylib                   0x00007fff884c528d _dispatch_client_callout + 8

10  libdispatch.dylib                   0x00007fff884ccef0 _dispatch_main_queue_callback_4CF + 333

11  CoreFoundation                      0x00007fff82dbe4f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9

12  CoreFoundation                      0x00007fff82d79714 __CFRunLoopRun + 1636

13  CoreFoundation                      0x00007fff82d78e75 CFRunLoopRunSpecific + 309

14  HIToolbox                           0x00007fff877cea0d RunCurrentEventLoopInMode + 226

15  HIToolbox                           0x00007fff877ce7b7 ReceiveNextEventCommon + 479

16  HIToolbox                           0x00007fff877ce5bc _BlockUntilNextEventMatchingListInModeWithFilter + 65

17  AppKit                              0x00007fff833f624e _DPSNextEvent + 1434

18  AppKit                              0x00007fff833f589b -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122

19  AppKit                              0x00007fff833e999c -[NSApplication run] + 553

20  AppKit                              0x00007fff833d4783 NSApplicationMain + 940

21  BLEChat Central                     0x0000000100005122 main + 34

22  libdyld.dylib                       0x00007fff87eea5fd start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException

根据 Xcode-Debugger 的说法,bug 应该在以下两种方法中:

-(void) bleDidReceiveData:(unsigned char *)data length:(int)length
{
    NSLog(@"Length: %d", length);

    data[length] = 0;
    NSString *str = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];



    NSLog(@"%@", str);

    static NSMutableString *message;

    if (message == nil)
        message = [[NSMutableString alloc] initWithString:@""];

    [message appendString:str];
    [message appendString:@"\n"];

    self.textView.string = message;
    [self.textView scrollRangeToVisible: NSMakeRange(self.textView.string.length, 0)];
}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    unsigned char data[20];

    static unsigned char buf[512];
    static int len = 0;
    NSInteger data_len;

    if (!error)
    {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@RBL_CHAR_TX_UUID]])
        {
            data_len = characteristic.value.length;
            [characteristic.value getBytes:data length:data_len];

            if (data_len == 20)
            {
                memcpy(&buf[len], data, 20);
                len += data_len;

                if (len >= 64)
                {
                    [[self delegate] bleDidReceiveData:buf length:len];
                    len = 0;
                }
            }
            else if (data_len < 20)
            {
                memcpy(&buf[len], data, data_len);
                len += data_len;

                [[self delegate] bleDidReceiveData:buf length:len];
                len = 0;
            }
        }
    }
    else
    {
        NSLog(@"updateValueForCharacteristic failed!");
    }
}

可能是在 OSX 应用程序中添加以下代码的解决方案?如果是这样,我必须把它放在哪里?

UInt16 value;
UInt16 pin;

for (int i = 0; i < length; i+=3) {
  pin = data[i];
  value = data[i+2] | data[i+1] << 8;
  NSLog(@"Pin: %d", pin);
  NSLog(@"Value %d",value);
}

我试图寻找解决方案三天,但我无法解决问题。如果你能帮助我,那就太好了:)

提前非常感谢和欢呼!

【问题讨论】:

  • >> 是右移操作。当您将右移 8 应用于 8 位值(即小于 256)时,您将得到 0 - 这似乎导致了 nil 附加错误。在您的第一次测试中,smoothedAX 的值是多少?你怎么知道 y 和 i 不正确?
  • smoothedAX 的值介于 -300 和 + 300 之间 - 此信息来自 Arduino 串行监视器。
  • 对不起,我没有注意到你有一个 16 位的值。奇怪的是,您有一个 unsigned int 但正在获得有符号值。无论如何,您用于将 16 位值作为两个字节发送的方法是正确的。您需要检查接收数据的代码以确定崩溃的原因
  • 我发现根据 Xcode-Debugger,错误位于 [message appendString:str] 和 [[self delegate] bleDidReceiveData:buf length:len] 行中以下问题? stackoverflow.com/questions/24207727/… 如果是这样,我应该在哪里实现这部分?谢谢@Paulw11
  • 是的,这个答案看起来像是重新创建 16 位值的正确方法。您的问题似乎是您使用的相同代码旨在接收文本字符串,而不是二进制数据。如果您更新您的问题以包含您评论中代码的方法,我可能会建议更改

标签: ios arduino bluetooth-lowenergy


【解决方案1】:

如果您只发送两个字节,那么最简单的方法是 -

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    int smoothedAX;

    if (!error)
    {
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@RBL_CHAR_TX_UUID]])
        {
            if (characteristic.value.length == 2) {
               const char *data=[characteristic.value bytes];
               smoothedAX=data[0] <<8 | data[1];
            }
            else {
               NSLog(@"Received %ld bytes",characteristic.value.length);
            }
        }
    }
    else
    {
        NSLog(@"updateValueForCharacteristic failed!");
    }
}

您将需要对 smoothedAX 做一些有用的事情 - 可能将其传递给另一个方法

【讨论】:

  • 您需要提供例外情况。我猜这是一个超出范围的错误,因为我不检查是否收到了两个字节。我很快就会更新我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多