Bluetooth API provide access to Bluetooth serial port connection between phone and another phone, phone and PC, phone and external Bluetooth device (for example external Bluetooth GPS device).
To allow Bluetooth enable the bluetooth capability. This is done by adding the following lines to build.yml:
capabilities: - bluetooth
Currently Bluetooth support has the following limitations:
There are two steps to make connection and start using of Bluetooth :
Rho::BluetoothManager.create_session
and setup callback where you receive result of connection. That callback will get events related to making connection(connect ok, canceled by user, error). Connection provided by platform specific UI.Rho::BluetoothManager.create_server_and_wait_for_connection
on server and Rho::BluetoothManager.create_client_connection_to_device
on client. On client you should specify server name(display name - not Bluetooth ID!) for connect. Specify callback - callback have the same parameters with Rho::BluetoothManager.create_session
. You can cancel connection process by Rho::BluetoothManager.stop_current_connection_process
Rho::BluetoothSession.set_callback
to process incoming session events from connected device (data received) or event related to this session connection(disconnect, errors).
Connection without UI worked only on iOS and Android platforms!
You can use the BluetoothManager API to make a connection between your Bluetooth-equipped device and another Bluetooth-equipped device.
create_server_and_wait_for_connection
), cancel the current connection.You can use the BluetoothSession API to manage a connection session between your Bluetooth-equipped device and another Bluetooth-equipped device.
This is not a complete example (for a link to a complete example, see the link below this example). This code just shows how you can make a connection and send/receive strings.
require 'rho/rhocontroller' require 'rho/rhobluetooth' class BluetoothController < Rho::RhoController @layout = :simplelayout $connected_device = nil def index render end def start_bluetooth if Rho::BluetoothManager.is_bluetooth_available() Rho::BluetoothManager.create_session(Rho::BluetoothManager::ROLE_CLIENT, url_for( :action => :connection_callback)) end end def send_string(str) Rho::BluetoothSession.write_string($connected_device, str) end def connection_callback if @params['status'] == Rho::BluetoothManager::OK $connected_device = @params['connected_device_name'] Rho::BluetoothSession.set_callback($connected_device, url_for( :action => :session_callback)) send_string('Hello friend !') end end def session_callback if @params['event_type'] == Rho::BluetoothSession::SESSION_INPUT_DATA_RECEIVED while Rho::BluetoothSession.get_status($connected_device) > 0 str = Rho::BluetoothSession.read_string($connected_device) # use received string end end end def close_all Rho::BluetoothSession.disconnect($connected_device) Rho::BluetoothManager.off_bluetooth() end end
You can find a complete example of using Bluetooth API in Rhodes-System-Api-Samples. See Bluetooth Chat Demo page - BluetoothChat. In this example you can see how to exchange text messages between two different devices. You also can use this example for connect to external Bluetooth device (external GPS device for example) or PC or Mac (use terminal to see and send messages).