Last active
August 29, 2015 14:07
-
-
Save aratakokubun/ac93a9828835a1181a15 to your computer and use it in GitHub Desktop.
PC・Android間のBluetooth通信 ref: http://qiita.com/aratakokubun/items/72f9db560835673e8129
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unique UUID for this application | |
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unique UUID for this application | |
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOCKET sockRecv; | |
SOCKADDR_BTH sockAddrBth = {0}; | |
sockRecv = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); | |
if (INVALID_SOCKET == sockRecv) { | |
return false; | |
} | |
sockAddrBth.addressFamily = AF_BTH; // For Bluetooth | |
sockAddrBth.port = BT_PORT_ANY; | |
if (SOCKET_ERROR == bind(sockRecv, (SOCKADDR *) &sockAddrBth, sizeof(sockAddrBth))) | |
{ | |
closesocket(sockRecv); | |
return false; | |
} | |
int size = sizeof(sockAddrBth); | |
getsockname(sockRecv, (SOCKADDR *) &sockAddrBth, &size); | |
CSADDR_INFO info = {0}; | |
info.LocalAddr.lpSockaddr = (LPSOCKADDR) &sockAddrBth; | |
info.LocalAddr.iSockaddrLength = sizeof(sockAddrBth); | |
info.iSocketType = SOCK_STREAM; | |
info.iProtocol = BTHPROTO_RFCOMM; | |
WSAQUERYSET set = {0}; | |
set.dwSize = sizeof(WSAQUERYSET); // Must be set to sizeof(WSAQUERYSET) | |
set.dwOutputFlags = 0; // Not used | |
set.lpszServiceInstanceName = "Server"; // Recommended. | |
set.lpServiceClassId = (LPGUID)&SerialPortServiceClass_UUID; // !ここのUUID(00001101-0000-1000-8000-00805F9B34FB)はAndroid側とマッチしている必要がある! | |
set.lpVersion = NULL; // Not used. | |
set.lpszComment = NULL; // Optional. | |
set.dwNameSpace = NS_BTH; // Must be NS_BTH. | |
set.lpNSProviderId = NULL; // Not required. | |
set.lpszContext = NULL; // Not used. | |
set.dwNumberOfProtocols = 0; // Not used. | |
set.lpafpProtocols = NULL; // Not used. | |
set.lpszQueryString = NULL; // not used. | |
set.dwNumberOfCsAddrs = 1; // Must be 1. | |
set.lpcsaBuffer = &info; // Pointer to a CSADDR_INFO. | |
set.lpBlob = NULL; // Optional. | |
if (WSASetService(&set, RNRSERVICE_REGISTER, 0) != 0) { | |
return false; | |
} | |
listen(sockRecv, 0); | |
SOCKADDR_BTH sab2; | |
int ilen = sizeof(sab2); | |
// 通信用のsocketを開く.このsocketで通信を行う. | |
acceptedSocket = accept(sockRecv, (SOCKADDR *)&sab2, &ilen); | |
// 接続用のsocketは必要ないので閉じる | |
closesocket(sockRecv); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void btSend(int* array, int len) { | |
int* netArray; | |
netArray = (int*)malloc(sizeof(int)*len); | |
for (int i = 0; i < len; i++) { | |
netArray[i] = htonl(array[i]); | |
} | |
send(acceptedSocket, (const char *)netArray, sizeof(int)*len, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment