|
8#
樓主 |
發表於 2013-8-1 11:37:36
|
只看該作者
本帖最後由 roboardgod 於 2013-8-1 11:42 編輯
回復 7# goldpower
okay..first, you need to install bluetooth driver on roboard
then, pair your devices
then, set com port for your device
Here's a simple sample code:
roboard (VC2008):
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- int main () {
- int i;
- DWORD dwBytesRead = 0;
- unsigned char Read_Packet1[2] = {0};
- HANDLE Serial = CreateFile(TEXT("\\\\.\\COM10"), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL); //to COM10
- if (Serial == INVALID_HANDLE_VALUE){
- printf("serial error");
- getchar();
- return 1;
- }
- DCB dcbSerialParams = {0};
- dcbSerialParams.BaudRate=115200;
- dcbSerialParams.ByteSize=8;
- dcbSerialParams.StopBits=ONESTOPBIT;
- dcbSerialParams.Parity=NOPARITY;
- if(!SetCommState(Serial, &dcbSerialParams)){
- printf("com setting error!!\n");
- goto End;
- }
- COMMTIMEOUTS timeouts={0};
- timeouts.ReadIntervalTimeout = MAXDWORD;
- timeouts.ReadTotalTimeoutConstant = 500;
- timeouts.ReadTotalTimeoutMultiplier = 20;
- timeouts.WriteTotalTimeoutConstant = 20;
- timeouts.WriteTotalTimeoutMultiplier = 20;
- if (!SetCommTimeouts(Serial, &timeouts)){
- printf("timeout error!\n");
- goto End;
- }
- while(!kbhit()){ //loop until keyboard click
- ReadFile(Serial, Read_Packet1, 1, &dwBytesRead, NULL);
- Read_Packet1[1] = '\0';
- if(Read_Packet1[0])
- printf("%s", Read_Packet1);
- Read_Packet1[0] = 0;
- }
- End:
- CloseHandle(Serial);
- getchar();
- return 0;
- }
複製代碼
Android phone(eclipse):
layout: one Button and one EditText
- package com.example.bluetooth_client;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Set;
- import java.util.UUID;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.app.Activity;
- import android.bluetooth.*;
- import android.content.Intent;
- public class MainActivity extends Activity {
- private static final String TAG = "DEBUG";
- private static final boolean D = true;
- private BluetoothAdapter mBluetoothAdapter = null;
- private BluetoothSocket btSocket = null;
- private OutputStream outStream = null;
- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- private static String address = "00:00:00:00:00:00"; //roboard's bluetooth device address
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button sendButton = (Button) findViewById(R.id.button);
- sendButton.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View arg0){
- TextView enteredText = (TextView)findViewById(R.id.entertext);
- String message = enteredText.getText().toString();
- byte[] msgBuffer = message.getBytes();
- try {
- outStream.write(msgBuffer);
- } catch (IOException e) {
- Log.e(TAG, "ON RESUME: Exception during write.", e);
- }
- }
- });
- if (D)
- Log.e(TAG, "+++ ON CREATE +++");
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
-
- if (mBluetoothAdapter == null) {
- Toast.makeText(this,"Bluetooth is not available.", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, 1);
- }
- if (!mBluetoothAdapter.isEnabled()) {
- Toast.makeText(this, "Please enable your BT and re-run this program.", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
- if (pairedDevices.size() > 0) {
- for (BluetoothDevice device : pairedDevices) {
- Log.e(TAG,device.getName() + "\n" + device.getAddress());
- }
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- BluetoothDevice device = null;
- if (D) {
- Log.e(TAG, "+ ON RESUME +");
- Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
- }
- if(BluetoothAdapter.checkBluetoothAddress(address))
- device = mBluetoothAdapter.getRemoteDevice(address);
- else{
- Log.e(TAG, "+++ address fail +++");
- }
-
- try {
- btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- Log.e(TAG, "ON RESUME: Socket creation failed.", e);
- }
-
- mBluetoothAdapter.cancelDiscovery();
- try {
- btSocket.connect();
- Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
- } catch (IOException e) {
- try {
- btSocket.close();
- } catch (IOException e2) {
- Log.e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
- }
- }
-
- if (D)
- Log.e(TAG, "+ ABOUT TO SAY SOMETHING TO SERVER +");
-
- try {
- outStream = btSocket.getOutputStream();
- } catch (IOException e) {
- Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
- }
-
-
- }
-
- @Override
- public void onPause() {
- super.onPause();
-
- if (D)
- Log.e(TAG, "- ON PAUSE -");
-
- if (outStream != null) {
- try {
- outStream.flush();
- } catch (IOException e) {
- Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
- }
- }
- try {
- btSocket.close();
- } catch (IOException e2) {
- Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
- }
- }
- }
複製代碼
and don't forget permission:
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
- <uses-permission android:name="android.permission.BLUETOOTH" />
複製代碼 |
|