Hi all, I am new to objective-c and mostly have experience in COBOL and Java.
As part of my internship project I have to connect a Mac computer with a custom build device over bluetooth.
All commands are sent through a bi-directional serial connection.
(9600 baud, 8 bit, no parity, no handshake)
The documentation on bluetooth was rather sparse but I did manage to get the devices to pair.
The most useful example I copied and tweaked as needed I found here.
On the Mac I am receiving data from the other device but it splits up the data incorrectly.
For instance when the state of the device changes it sends the following command over bluetooth:
w31CRLF, where w stands for state change and 31 are the parameters and CRLF is a Carriage Return followed by a LineFeed.
When I log the incoming data using the rfcommChannelData delegate method it only takes the alphanumeric value and logs the numerical + Carriage return separately, following by the LineFeed as in the following output in the console:
note that I added the length passed to rfcommChannelData in the log to doublecheck the incorrect splitting up
the openConnection delegate method as I have it atm:
the rfcommChannelData delegate method:
I have used the following encoding types as well during test but with the same result:
NSASCIIStringEncoding
NSUTF8StringEncoding
NSWindowsCP1250StringEncoding
I have spend a lot of time looking for a solution over the past two days so I would truely appreciate any help
As part of my internship project I have to connect a Mac computer with a custom build device over bluetooth.
All commands are sent through a bi-directional serial connection.
(9600 baud, 8 bit, no parity, no handshake)
The documentation on bluetooth was rather sparse but I did manage to get the devices to pair.
The most useful example I copied and tweaked as needed I found here.
On the Mac I am receiving data from the other device but it splits up the data incorrectly.
For instance when the state of the device changes it sends the following command over bluetooth:
w31CRLF, where w stands for state change and 31 are the parameters and CRLF is a Carriage Return followed by a LineFeed.
When I log the incoming data using the rfcommChannelData delegate method it only takes the alphanumeric value and logs the numerical + Carriage return separately, following by the LineFeed as in the following output in the console:
Code:
2013-05-15 16:56:20.581 TimeBurger[4144:303] received: w (1)
2013-05-15 16:56:20.589 TimeBurger[4144:303] received: 33
(4)
2013-05-15 16:56:21.568 TimeBurger[4144:303] received: w (1)
2013-05-15 16:56:21.569 TimeBurger[4144:303] received: 31
(4)
2013-05-15 16:56:22.190 TimeBurger[4144:303] received: w (1)
2013-05-15 16:56:22.191 TimeBurger[4144:303] received: 21
(4)
note that I added the length passed to rfcommChannelData in the log to doublecheck the incorrect splitting up
the openConnection delegate method as I have it atm:
Code:
//Open the connection.
-(BOOL) openConnection:(BluetoothRFCOMMChannelID *) chanId{
IOBluetoothRFCOMMChannel *channel;
if ([_btDevice openRFCOMMChannelAsync:&channel withChannelID:*chanId delegate:self] != kIOReturnSuccess) { // after connection it is established.. the delegates methoed are triggered.
NSLog(@"Couldn't open channel");
return NO;
}
[channel setSerialParameters: 9600 dataBits: 8 parity: kBluetoothRFCOMMParityTypeNoParity stopBits: 2];
[channel closeChannel];
return YES;
}
the rfcommChannelData delegate method:
Code:
//reading data from rfcomm
- (void)rfcommChannelData:(IOBluetoothRFCOMMChannel*)rfcommChannel data:(void *)dataPointer length:(size_t)dataLength {
NSString *newStr = [[NSString alloc] initWithBytes:dataPointer length:dataLength encoding:NSUTF8StringEncoding];
NSLog(@"received: %@ (%zu)",newStr, dataLength);
// NSLog(@"received lengt: %zu",dataLength);
}
I have used the following encoding types as well during test but with the same result:
NSASCIIStringEncoding
NSUTF8StringEncoding
NSWindowsCP1250StringEncoding
I have spend a lot of time looking for a solution over the past two days so I would truely appreciate any help
Last edited: