언어 설정

Menu
Sites
Language
기어S2 블루투스 연결이 안됩니다

function chatServiceSuccessCb(handler) {
   alert("Chat service registration was successful!");
   ServiceHandler = handler;

   //point 1
   handler.onconnect = function(socket) {
      alert("Client is connected: " + socket.peer.name + "," + socket.peer.address);
      socket.onmessage = function() {
          var data = socket.readData();
          // Handle message code goes here
          //....
          alert(data);
      };

      // Expected close
      socket.onclose = function() {
          console.log('The socket is closed.');
      };

     //point2
      Socket=socket;
   };
}

===================================================================

point1에 있는 handler와 ServiceHandler 변수가 유효한지까지 확인하였으며

onconnect함수 정의 부분에 alert()함수부터 동작하지 않습니다. 제가 놓친 부분이 있나요?

제 프로젝트 시나리오는 

 if(!device.isBonded) adapter.createBonding(device.address, onBondingSuccess, onBondingError);

adapter.registerRFCOMMServiceByUUID(SERVICE_UUID, "SERVICE", chatServiceSuccessCb,
                                  // Error handler
                              function(e) {
                                   alert( "Could not register service record, Error: " + e.message);
                              });

이렇게 되어있는데 연결시나리오 부분에서 잘못된 부분이 있나요?

또한 point2 부분처럼 소켓을 전역변수에 저장하여 writedata를 쓰려고 합니다.

이것이 가능한지요?

 

Responses

1 댓글
AVSukhov

Hello,

You can use following algorithm:

  1.        Get Bluetooth adapter (getDefaultAdapter)
  2.        On first device discover available devices (discoverDevices)
  3.        In success callback create bounding with necessary device (createBounding)
  4.        In success callback on first device register service (registerRFCOMMServiceByUUID) for connection between one app you can hardcode bluetoothUUID
  5.        On second device get list of known device (getKnownDevices) and get bounding device
  6.        On second  device set connection to bounding device (connectToServiceByUUID) with the same UUID (from step 4). onconnect function is called after this step.

Also add necessary privilege.