在 XCode 中创建基于终端的蓝牙监视器?

发布于 2024-10-05 00:56:49 字数 1710 浏览 3 评论 0原文

我想创建一个连接到蓝牙设备并输出蓝牙设备发送的任何命令的终端应用程序。到目前为止,我能够扫描并输出可用设备的列表。

任何方向将不胜感激 - 这可能吗?我现在应该看什么?我尝试使用 BluetoothDeviceAddress 和 IOBluetoothL2CAPChannelGetDevice 但尚未取得任何成功。

到目前为止,这是我的代码:

#include <Foundation/Foundation.h>
#include <Cocoa/Cocoa.h>
#include <IOBluetooth/objc/IOBluetoothDeviceInquiry.h> 
#include <IOBluetoothUI/IOBluetoothUI.h>

int main (int argc, const char * argv[]) { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSLog(@"start bluetooth search"); 

    IOBluetoothDeviceInquiry *d = [[IOBluetoothDeviceInquiry new] init]; 
        [d setInquiryLength: 5]; 
        [d setUpdateNewDeviceNames: TRUE]; 
        [d start];

    [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 7]]; 
        [d stop]; 
        NSArray *deviceList = [d foundDevices]; 

    NSLog(@"found %d devices", [deviceList count]); 

    for(int i=0;i < [deviceList count]; i++) {

        NSScanner *theScanner = [NSScanner scannerWithString:[NSString stringWithFormat:@"%@", [deviceList objectAtIndex:i]]];


        NSString *tagDeviceName = @"mName - ";
        NSString *tagEndLine = @"\n";       

        NSString *currentDeviceName;

        // extract the mName from the current array value
        while ([theScanner isAtEnd] == NO) {
            [theScanner scanUpToString:tagDeviceName intoString:NULL];
            [theScanner scanString:tagDeviceName intoString:NULL];
            [theScanner scanUpToString:tagEndLine intoString:&currentDeviceName];           
        } // end [theScanner isAtEnd]

        NSLog(@"device name: %@", currentDeviceName);

    }


    [pool release]; 
    return 0; 
}

I want to create a Terminal application that connects to a Bluetooth Device and outputs any commands that are sent out by the bluetooth device. So far, I am able to scan and output a list of available devices.

Any direction would be greatly appreciated - is this even possible? What should I be looking at now? I tried to use BluetoothDeviceAddress and IOBluetoothL2CAPChannelGetDevice but haven't had any success yet.

Here is my code so far:

#include <Foundation/Foundation.h>
#include <Cocoa/Cocoa.h>
#include <IOBluetooth/objc/IOBluetoothDeviceInquiry.h> 
#include <IOBluetoothUI/IOBluetoothUI.h>

int main (int argc, const char * argv[]) { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSLog(@"start bluetooth search"); 

    IOBluetoothDeviceInquiry *d = [[IOBluetoothDeviceInquiry new] init]; 
        [d setInquiryLength: 5]; 
        [d setUpdateNewDeviceNames: TRUE]; 
        [d start];

    [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 7]]; 
        [d stop]; 
        NSArray *deviceList = [d foundDevices]; 

    NSLog(@"found %d devices", [deviceList count]); 

    for(int i=0;i < [deviceList count]; i++) {

        NSScanner *theScanner = [NSScanner scannerWithString:[NSString stringWithFormat:@"%@", [deviceList objectAtIndex:i]]];


        NSString *tagDeviceName = @"mName - ";
        NSString *tagEndLine = @"\n";       

        NSString *currentDeviceName;

        // extract the mName from the current array value
        while ([theScanner isAtEnd] == NO) {
            [theScanner scanUpToString:tagDeviceName intoString:NULL];
            [theScanner scanString:tagDeviceName intoString:NULL];
            [theScanner scanUpToString:tagEndLine intoString:¤tDeviceName];           
        } // end [theScanner isAtEnd]

        NSLog(@"device name: %@", currentDeviceName);

    }


    [pool release]; 
    return 0; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-10-12 00:56:49

下一步将是:
1.通过SDP发现设备上可用的服务
2. 连接服务并读取数据。

您可以使用 SPP 配置文件进行数据发送/接收 - 假设您要连接的设备正在使用此配置文件发送数据。

The next step will be :
1. Discover the services available on the device via SDP
2. Connect to the service and read the data.

You can use the SPP profile for data send / receive - assuming that the device you are connecting to is using this profile to send the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文