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