LoginSignup
9
8

More than 5 years have passed since last update.

objective-cとmacrubyでbluetooth deviceの存在確認をする

Last updated at Posted at 2013-10-20

objective-c版

main.m
#import <Foundation/Foundation.h>
#import <IOBluetooth/objc/IOBluetoothDevice.h>


int main(int argc, const char * argv[])
{

    NSLog(@"start");
    NSString *addrStr = @"xx:xx:xx:xx:xx:xx"; // bluetooth deviceのMAC Address

    IOBluetoothDevice *device = [IOBluetoothDevice deviceWithAddressString:addrStr];

    // タイムアウト3秒
    [device openConnection:nil withPageTimeout:3000 authenticationRequired:false];

    if ([device isConnected]) {
            NSLog(@"connected !!!");
    } else {
            NSLog(@"not connected");
    }

    [device closeConnection];

    return 0;
}

macruby版

macruby-sample.rb
#! /usr/bin/env macruby

framework 'Foundation'
framework 'IOBluetooth'

device = IOBluetoothDevice.deviceWithAddressString "xx:xx:xx:xx:xx:xx"
device.openConnection(nil, withPageTimeout: 3000, authenticationRequired: false)

if device.isConnected
  puts 'connected!!!'
else
  puts 'not connected.'
end

device.closeConnection
9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8