Prerequisite:
—————-
Two Bluetooth-enabled device with Bluetooth switched on manually to make the device visible to other devices.
Internal Functionality:
—————————–
Steps we need to follow to make communication between 2 devices using Bluetooth.
– Before creating the connection the two devices need to be paired.
– In order to pair the two devices a password has to be exchanged between the two devices.
– Then the Device 1 (Any one of the device) should open the server socket. That means, it should behave like a master in the communication
– Device 2 should open the client socket. That means, it behaves like a slave to the recently created server (the first device) in the communication.
– Once the Bluetooth pairing has occurred, data can be exchanged between the devices using Rhomobile BluetoothSession API.
|
How to create the Application:
———————————-
– Open Rhostudio
– Go to File- New – Project
– select Rhodes/Rhomobile Application
– Enter the Application name
– Right click on App
– select New -Rhodes Model
– Enter Model name and Model Attribute
– Click on Finish
Important:
———
-Open build.yml(present inside root folder i.e project )
-Go to Capability-Add
-select Bluetooth
-press OK
For Android:
————
Add <uses-permission android:name=”android.permission.BLUETOOTH” /> to application manifest file.
(path:-Rhostudio installation folder\ruby\lib\ruby\gems\1.8\gems\rhodes-3.3.2\platform\android\Rhodes\AndroidManifest.xml)
Now write the following code inside a view page (lets say index.erb) present inside the model.
index.erb
|
--------- <INPUT type="button" value="Connect as Server" name="server" onClick="onConnectServer();"> <INPUT type="button" value="Connect as Client" name="client" onClick="onConnectClient();"> <form method="POST" action="<%=url_for(:action => 'onSend')%>" > <input id="message" type="text" name="message" value =""></input> <a href="#" onclick="document.forms[0].submit(); return false;">SEND</a> </form> <script> // Calls the controller method ConnectAsServer to connect as a server function onConnectServer() { $.get('/app/BluetoothChat/ConnectAsServer', {}); return false; } // Calls the controller method ConnectAsClient to connect as a client function onConnectClient() { $.get('/app/BluetoothChat/ConnectAsClient', {}); return false; } </script>
And Now write the following code inside the controller present inside the created Model.
Inside Controller:
--------------- $connected_device_name = nil //Stores the connected device Name #Creates a Bluetooth session as a Server #It opens a server socket and wait to listen for incoming connections # When one connection is available it calls the create_session() to create the connection def ConnectAsServer #Check whether any device is conected or not if $connected_device_name == nil # Scan for other remote Bluetooth devices and on select of the device it Creates a Bluetooth session # Returns OK or ERROR Rho::BluetoothManager.create_session(Rho::BluetoothManager::ROLE_SERVER, url_for(:action => :CreateSessionCallback) ) else # Disconnects the currenly connected Device DisconnectSession end end #Creates a Bluetooth session as a client def ConnectAsClient if $connected_device_name == nil #Creates the session Rho::BluetoothManager.create_session(Rho::BluetoothManager::ROLE_CLIENT, url_for(:action => :CreateSessionCallback) ) else # Disconnects the currenly connected Device DisconnectSession end end def DisconnectSession #Disconnect from the device. #Returns OK or ERROR. Rho::BluetoothSession.disconnect($connected_device_name) $connected_device_name = nil end # Callback method called after the Bluetooth session is created or canceled # Prameters received in the callback: # 1.status : OK / ERROR / CANCEL # 2.connected_device_name : The name of the device connected via Bluetooth. def CreateSessionCallback $connected_device_name = @params['connected_device_name'] if @params['status'] == Rho::BluetoothManager::OK #Set the Bluetooth session callback to Transfer data to the connected devices Rho::BluetoothSession.set_callback($connected_device_name, url_for(:action => :SessionCallback)) end end # Callback to the set_callback of BluetoothSession # Parameters received in the callback: # 1.connected_device_name : Name of connected device; # 2.event_type : SESSION_INPUT_DATA_RECEIVED or ERROR or SESSION_DISCONNECT def SessionCallback eventType = @params['event_type'] if eventType == Rho::BluetoothSession::SESSION_INPUT_DATA_RECEIVED # receive data from the connected device. OnDataReceived end end # This method receives the data from the connected device def OnDataReceived # Get the session status. # Returns the size of the data received by the bluetooth connection or -1 if error or 0 if empty while Rho::BluetoothSession.get_status($connected_device_name) > 0 # Read data from the connected device. # Returns an array of bytes. message = Rho::BluetoothSession.read_string($connected_device_name) end end # This method sends the data to the connected device def onSend message = @params['message'] # Write data to the connected device. Rho::BluetoothSession.write_string($connected_device_name, message) end