Pairing bluetooth device with user defined PIN-QT - bluetooth

I am trying to make a bluetooth pairing application with Qt5. i want to pair with a HID device which is having default pin 1234(say), i want to enter this pin in my application.
QBluetoothLocalDevice localDevice;
localDevice.requestPairing(address, QBluetoothLocalDevice::Paired);
connect(&localDevice, SIGNAL(pairingDisplayPinCode(QBluetoothAddress,QString)),
this, SLOT(displayPinCode(QBluetoothAddress,QString)));
connect(&localDevice, SIGNAL(pairingDisplayConfirmation(QBluetoothAddress,QString)),
this, SLOT(displayParingConfirmation(QBluetoothAddress,QString)));
void BluetoothDevice::displayPinCode(const QBluetoothAddress &address, QString pin)
{
localDevice.pairingConfirmation(true);
}
void BluetoothDevice::displayParingConfirmation(const QBluetoothAddress &address, QString pin)
{
localDevice.pairingConfirmation(true);
}
this code emit some random pin.
Is there any provision to input a pin and pair device using that pin.
thanks in advance.
(i am using Ubuntu 14.10)

Please refer the files listed below from here:
bluetoothagentadaptor.cpp
bluetoothagentadaptor.h
bluetoothbaseagent.cpp
bluetoothbaseagent.h

Related

Is there any way to send this Keyboard.Press(); command wireless to my PC via Bluetooth?

I have already been able to implement the wireless data sending of my Arduino Micro board via Bluetooth. I used following code to send my sensor data from the arduino board to PC. But I want it to be able to do a Keyboard.Press(); according to the data I receive from my sensors attached to the Micro board.
Is there any way to send this Keyboard.Press(); command wireless to my PC via Bluetooth ?
I use Arduino Micro with HC-05 to send data to the PC wireless.
#include <SoftwareSerial.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU9250.h"
MPU9250 accelgyro;
I2Cdev I2C_M;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;
float Axyz[3];
const int rxPort = 8; // connected to Bluetooth TX
const int txPort = 9; // connected to Bluetooth RX
SoftwareSerial myConnection = SoftwareSerial(rxPort, txPort);
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU9250 connection successful" : "MPU9250 connection failed");
delay(1000);
myConnection.begin(9600);
}
void loop()
{
myConnection.print(mapX);
myConnection.print(" , ");
myConnection.println(mapY);
////Keyboard.press('a');
}
For bluetooth v2 you need a HID-enabled bluetooth module. Once I knew this product
https://www.sparkfun.com/products/retired/10938
but now they retired it.
If you want to move to bluetooth v4 I don't know. Searching on the web it appears that you will need to implement "HID over GATT profile" (HOGP).
I found this library for some ble chips (nRF8001, nRF2740/nRF2741) which has examples of a keyboard over bluetooth; otherwise if you already have a BT4 board try looking for libraries which implement that profile, or try porting it to your platform.
Of course this only applies to bluetooth 4 boards, since bluetooth 2 has some fixed "classes" (so you can't turn a BT-serial adapter into a BT-HID device).

How can I connect a EV3 mindstorms via bluotooth to a unity game using unityscript?

I am making a race game in Unity with Unityscript and I made a steer with the lego mindstorms EV3 robot. I let te robot send information by bluetooth to the game, but I can't find how I can do that.
I already have the code for the bluetooth running and working a C#, but know I need to know how to translate it to unityscript.
I already tried to find it on google, but I only seem to get some software to hack the robot, but not to make code in unityscript for connecting the steer.
Under here stands the C# code:
// EV3: The EV3Messenger is used to communicate with the Lego EV3.
private EV3Messenger ev3Messenger;
// EV3: Create an EV3Messenger object which you can use to talk to the EV3.
ev3Messenger = new EV3Messenger();
// EV3: Connect to the EV3 serial port over Bluetooth.
// If the program 'hangs' on a call to ev3Messenger.Connect,
// then your EV3 is not paired with your PC yet/anymore.
// To pair: Remove the EV3 from the Windows Bluetooth device list and add it again.
ev3Messenger.Connect("COM3"); // Hardcoded serial port: put the serial port
// of the Bluetooth connection to your EV3 here!
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// Unload any non ContentManager content here
// EV3: Disconnect
if (ev3Messenger.IsConnected)
{
ev3Messenger.Disconnect();
}
}
// EV3: send Brake message to mailbox with name "MakeNoise"
if (ev3Messenger.IsConnected)
{
ev3Messenger.SendMessage("MakeNoise", "Brake");
}
// Game can be controlled by both the arrow keys and the Steer, gas and brake paddle of the connected EV3
UpdatePaddlePositionUsingKeys();
UpdatePaddlePositionUsingEV3();
base.Update(gameTime);
}
///Steer update
private void UpdatePaddlePositionUsingEV3()
{
if (ev3Messenger.IsConnected)
{
// EV3: Receive a new command from mailbox "COMMAND" of the EV3
// and use it to change the direction of the paddle or to exit the game.
EV3Message message = ev3Messenger.ReadMessage();
if (message != null
&& message.MailboxTitle == "Command")
{
if (message.ValueAsText == "")
{
}
{
ev3Messenger.Disconnect();
Exit();
}
}
}
}
I hope that you know where I can find how I can do this or even help me further.
If you want the original code for a small pong game where I got my inspiration from, just comment it.
I hope you can help me.
Here are some useful links with documentation on the EV3 firmware :
Firmware Documentation
LMS2012 Documentation
HDK/SDK
In particular, you need to learn how to send direct commands and then use that to read and write bluetooth mailboxes.
For communicating with the COM port itself using javascript, just do a little searching. For example, I found this SO question which has quite a few different ideas.
As part of c4ev3, We open-sourced our EV3 uploader, which can also be used to send connection-agnostic commands to the device.
Here is how you would move the motors in Perl (Complete version):
use IPC::Open2;
print open2(\*EV3OUT, \*EV3IN, "ev3 tunnel") or die "couldn't find: %!";
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 09 50 A6 00 09\n";
This would probe for an EV3 accessible over USB, Bluetooth or WiFi and connect to it, then send the direct messages associated with turning the motors. For more information on the direct commands protocol check out LEGO's Communication Developer Manual and David Lechner's answer.
Alternatively, you can write a C program for the EV3 with c4ev3 and communicate with that. That way you got a nicer looking C-API you can use.

Unable to detect Seeedstudio Bluetooth Shield

I have a problem regarding Seeedstudio Bluetooth shield http://www.seeedstudio.com/depot/Bluetooth-Shield-p-866.html
I can't detect its presence by any other devices.
The code I uploaded to Arduino is a standard example for slave device from the library:
/* Upload this sketch into Seeeduino and press reset*/
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
#define DEBUG_ENABLED 1
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
}
void loop()
{
char recvChar;
while(1)
{
if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
recvChar = blueToothSerial.read();
Serial.print(recvChar);
}
if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
recvChar = Serial.read();
blueToothSerial.print(recvChar);
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
blueToothSerial.flush();
}
I've uploaded it to Arduino UNO, connected the shield and... Nothing.
LED marked as D1 is blinking green, and D2 is switched off. The device is not detected by none of the three devices I've tried (two computers and a smartphone).
By "not detected" I mean "hcitool returns nothing and OS based search for Bluetooth devices reports nothing". All three devices can detect each other without any problems.
I tried to connect it to other UNO board (in case the first one was damaged), but the result is the same.
I thought that the shield is somehow at fault, so I had it replaced by a new one - but the results are still the same.
Summing up:
3 extrnal devices
2 Arduinos
2 shields
Tested in all possible combinations, and still no success.
The device is powered up, because when I set it to send it's status to A1 analog port I always read 0 instead of a random value.
The only logical conclusion is that there is something wrong with the code above, but every google search I've made pointed me exactly to that file. It's from official wiki and in every example I've found. I've tried to contact Seeedstudio about it, but they didn't have anything of value to add ("try rebooting until it works").
Has anyone had similar problem, or has any advice what's wrong with the code?
I've managed to solve the problem on my own.
Two steps were required:
First of all I had to use hardware serial port (ports 0 and 1 on Arduino UNO), because for some reason SoftwareSerial doesn't work well with this shield.
Then I had to battery as power supply, since serial communication via ports 0 and 1 has lower priority than USB serial.
The disadvantage of this solution is that you lose USB communication with your PC, but fortunately I didn't need it.
EDIT: To avoid any confusion, here is an example of the code which worked for me:
void setup()
{
setupBlueToothConnection();
Serial.flush();
}
void loop()
{
for (char i = 0; i <= 254; i++)
{
Serial.print(i);
delay(1000);
}
}
void setupBlueToothConnection()
{
Serial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
Serial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
Serial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
Serial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
Serial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
Serial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
//Serial.println("The slave bluetooth is inquirable!");
delay(2000); // This delay is required.
Serial.flush();
}
Please note that the modem sends answers to some of the commands, you should clean Serial buffer before reading from it.
Also, the jumpers on the shield should be connected like this: BT_TX to pin 0 and BT_RX to pin 1.

Sending commands from custom made Bluetooth device to android phone to control music player

I have created a simple Bluetooth device using following components
HC05 module
Arduino Uno board (with re-programmable micro-controller)
I am wondering if it is possible to send commands from my BT device, as if these commands were sent from Bluetooth headset?
What I mean is:
we send 0x00000055 keycode - and the music pauses
(KEYCODE_MEDIA_PLAY_PAUSE)
we send 0x00000058 - previous song starts playing
(KEYCODE_MEDIA_PREVIOUS)
...
Here is the full list of keycodes which android uses: http://developer.android.com/reference/android/view/KeyEvent.html
I can probably create a separate app, which will read incoming commands and simulate headset button presses, but that is not what I want. As far as I'm concerned - some of the headsets are plug-and-play, meaning that no additional apps must be installed on android device. Here is the code I am currently use to send commands to Android phone:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
enum { LED_PIN = 6 };
enum LedState { LED_ON, LED_OFF, LED_BLINK };
LedState led_state;
void setup()
{
led_state = LED_OFF;
pinMode(LED_PIN, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
const int COMMAND_MUSIC = 85;
void loop()
{
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
delay(10000);
// trying to play or pause once in 10 seconds
BTSerial.write(0x00000055);
//BTSerial.print(0x00000055, HEX);
}
Both devices are paired but music player on my phone stays unaffected by these commands..Is it possible to control music player without creating a side app for "incoming commands from BT"?
Question is if your board supports AVRCP controller BT profile?
If it does you "only" need to connect against your phones AVRCP target BT profile. When you have a AVRCP BT connection there is specified commands how to pause and skip songs.
This is how the "plug and play" headset does.
Read more about Bluetooth profiles.
http://en.wikipedia.org/wiki/Bluetooth_profile
Looking at your code you have set up a serial link towards a phone.
This link uses SPP profile and you will only be able to send raw data over that link.
If this is the only profile that your BT stack on your Arduino Uno board have you will be forced to create an application on the phone side to be able to read the raw data and do something with it e.g. pause music.
Hope this cleared things little for you.
Probably it is to late for you, but maybe I can help someone else.
Firstly, Bluetooth devices like BT headphones, keyboards etc, are known as HID (Human Interface devices). HC05 are not one of this out of the box, but there is a solution introduced by Evan Kale (link: https://www.youtube.com/watch?v=BBqsVKMYz1I) how to update one of this using serial port connection.
Other solution is to buy BT HID module, but they are more expensive (about 10 times)

Checking if the bluetooth module on my Arduino board works

I'm trying to have my Arduino UNO board to work with a BlueSmirf Gold (http://www.sparkfun.com/products/10268).
I wired it as explained on various tutorial (for example here: http://www.instructables.com/id/how-to-Control-arduino-by-bluetooth-from-PC-pock/)
I've set the baud rate to 9600 as explained here: http://forum.sparkfun.com/viewtopic.php?p=94557
I manage to connect to it using the default Arduino serial terminal, ZTerm and my phone (using Amarino). In every cases, the green light on the bluetooth modem turns on, so until there it looks good.
The main problem is that my modem does not seem to be able to send/receive anything (the only time I've had any response was when I've set the baud rate to 9600).
For example, I have this code (simplified here, but the main idea is there):
int out_pin = 2;
String readLine() {
char command[100];
int i = 0;
if(Serial.available()){
delay(100);
while( Serial.available() > 0 && i< 99) {
command[i++] = Serial.read();
}
command[i++]='\0';
Serial.flush();
}
Serial.print("command: ");
Serial.println(command);
return (String) command;
}
void menu() {
if (Serial.available() <= 0) {
return;
}
String command = readLine();
// Do thing based on the command
}
void setup() {
pinMode(out_pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
menu();
}
Logically, when I send something via the terminal, I should get it back (which is what happens when using the usb serial).
When I connect to the board via Bluetooth, it just stays silent.
I also tried this piece of code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Spam ...");
}
Works fine when using the usb serial, but I get nothing when using bluetooth to connect to my board.
With both codes, I also tried to use the monitor tool in Amarino to send messages to the board, but it seems that it never got it.
I've tried various other things:
- do not have the USB serial available (I powered the Arduino board via USB but using a plug wall adapter. I'll try later on with a 9V battery but I don't have it available at the moment)
- do not connect CTS-1 to RTS-0 in the modem (some tutorial tell to connect them, the other don't. So as I doubted I tried both solutions).
The only time I've had something that looked like a communication was with this setup:
Arduino - Phone connected via Bluetooth
The Amarino monitoring was on
Arduino - Computer connected via the USB serial
When uploading the new code to my board, some parts of it were displayed on the monitoring tool on the phone.
It happened one or twice and I can't reproduce it now.
I'm pretty sure I've done something wrong somewhere (it seems at least it's the most logical explanation) but I'm also wondering if it could not be a problem with the Bluetooth model (I mean, even the sample tutorials do not work).
So the questions are:
is there something I missed/forgot to do that could help me solve the
problem ?
if not: is there a simple way to check that my Bluetooth modem
works fine ?
Thanks,
Vincent
I still don't have the answer on the second question ("is there a simple way to check that my Bluetooth modem works fine ?") but I've finally be able to send/receive messages from the Bluetooth modem.
As I was guessing (at least how I understand it) that was a problem with the two serials (Bluetooth and USB) on the same board.
To solve that, I've plugged the BT TX-1 on pin digital 5, RX-0 on digital 3 and used the following code (based on the SoftwareSerial tutorial):
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(5, 3);
void setup()
{
// Start the hardware serial port
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
bluetooth.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (bluetooth.available() > 0) {
char inByte = bluetooth.read();
Serial.write(inByte);
}
}
It sends all entries received from Bluetooth on the default serial (in my case USB).
I've checked with Amarino and messages sent from my phone are displayed in the Arduino serial monitor.
Same problem here. I tried connect vice versa the 0 and 1, RX and TX(i.e. RX to RX and TX to TX) and I got some communication of gibberish as opposed to nothing at all.

Resources