2016年2月7日 星期日

[Arduino] Android透過藍芽控制LED

先來看一段效果
                

用文字表示架構


                 BlueTooth

Android -------------->  BlueTooth Module(HC-05)  --->Arduino ---> LED


分2部份:

第一部份是Arduino 的部份,它必須去控制LED與接收藍芽的訊號。

Arduino Code


#include <SoftwareSerial.h>

#include <Wire.h>

 

// the maximum received command length from an Android system (over the bluetooth)

#define MAX_BTCMDLEN 128

 

// 建立一個軟體模擬的序列埠; 不要接反了!

// HC-05    Arduino

// TX       RX/Pin10

// RX       TX/Pin11

SoftwareSerial BTSerial(10,11); // Arduino RX/TX


 

byte cmd[MAX_BTCMDLEN]; // received 128 bytes from an Android system

int len = 0; // received command length

int LED_Red=12; 
int LED_Green=13;

void setup() {

   pinMode(LED_Red, OUTPUT);
   pinMode(LED_Green, OUTPUT); 

   digitalWrite(LED_Red, LOW);
digitalWrite(LED_Green, LOW);

    Serial.begin(9600);   // Arduino起始鮑率:9600

    BTSerial.begin(9600); // HC-05 出廠的鮑率:每個藍牙晶片的鮑率都不太一樣,請務必確認

}

 

void loop() {

    char str[MAX_BTCMDLEN];

    int insize, ii;  

    int tick=0;

    while ( tick<MAX_BTCMDLEN ) { // 因為包率同為9600, Android送過來的字元可能被切成數份

        if ( (insize=(BTSerial.available()))>0 ){ // 讀取藍牙訊息

            for ( ii=0; ii<insize; ii++ ){

                cmd[(len++)%MAX_BTCMDLEN]=char(BTSerial.read());

            }

        } else {

            tick++;

        }

    }

    if ( len ) { // 用串列埠顯示從Android手機傳過來的訊息

        sprintf(str,"%s",cmd);

        Serial.println(str);

        Serial.println(cmd[0] );
        Serial.println(cmd[1]);
        
        if (cmd[1]==49)
        {
           digitalWrite(LED_Red, HIGH);
        }
        else if (cmd[1]==48)
        {
          digitalWrite(LED_Red, LOW);
        }
        
        if (cmd[0]==49)
        {
           digitalWrite(LED_Green, HIGH);
        }
        else if (cmd[0]==48)
        {
          digitalWrite(LED_Green, LOW);
        }

        cmd[0] = '\0';

    }

    len = 0;

}

Arduino 線路圖

Arduino 3.3  -->   HC-05 VCC

Arduino GND --> 麵包板GND

Arduino  pin10  ---> HC-05  TX

Arduino pin 11 ---> HC-05 RX

Arduino pin 12 ----> Red LED 正極

Arduino pin 13 ----> Green LED 正極

LED 負極接在麵包版負極。

HC-05 GND 接麵包板GND

第二個部份是Android透過藍芽傳輸控制訊號。


package com.example.boywhychen.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    BluetoothAdapter mBtAdapter;
    ListView list;
    Set<BluetoothDevice>  deviceSet;
    BluetoothDevice device;
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    String fiilliste[];
    InputStream mmInStream;
    OutputStream mmOutStrem;
    byte[] command=new byte[4];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        list=(ListView)findViewById(R.id.listView);
        command[2]=(byte)0x0D;
        command[3]=(byte)0x0A;
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (BluetoothDevice bd : deviceSet) {
                    if (bd.getName().equals(fiilliste[position])) {
                        device = bd;
                    }
                }
                try {

                    BluetoothSocket mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                    mmSocket.connect();  // blocking call

                    mmInStream = mmSocket.getInputStream();
                    mmOutStrem = mmSocket.getOutputStream();

                } catch (IOException e) {

                }
            }
        });


        android.support.v7.widget.SwitchCompat red=(android.support.v7.widget.SwitchCompat)findViewById(R.id.red);
        red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked)
                {
                    command[1]=49;
                }
                else
                {
                    command[1]=48;
                }
                if (mmOutStrem!=null)
                {

                    try {
                        mmOutStrem.write(command);
                        mmOutStrem.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        android.support.v7.widget.SwitchCompat green=(android.support.v7.widget.SwitchCompat)findViewById(R.id.green);

        green.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    command[0]=49;
                }
                else
                {
                    command[0]=48;
                }
                if (mmOutStrem!=null)
                {

                    try {
                        mmOutStrem.write(command);
                        mmOutStrem.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        // 檢查藍牙是否開啟
        if (!mBtAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }

        mBtAdapter.startDiscovery();
        // 搜尋到藍牙裝置時,呼叫我們的函式
        IntentFilter filter = new IntentFilter( BluetoothDevice.ACTION_FOUND );
        this.registerReceiver( mReceiver, filter );

        // 搜尋的過程結束後,也呼叫我們的函式
        filter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_FINISHED );
        this.registerReceiver(mReceiver, filter);

    }

    private BroadcastReceiver mReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            ArrayList<String> stringarray=new ArrayList<String>();
            ArrayAdapter<String> adapter;

            if (intent.getAction()== BluetoothDevice.ACTION_FOUND)
            {
                deviceSet= mBtAdapter.getBondedDevices();
                for (BluetoothDevice bd:deviceSet)
                {
                    stringarray.add(bd.getName());
                }
            }
            fiilliste=new String[stringarray.size()];
            stringarray.toArray(fiilliste);

            adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, fiilliste);

            list.setAdapter(adapter);
        }
    };

}

layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.boywhychen.bluetooth.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/listView"
        android:layout_marginTop="10dp"
        android:text="Red LED" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/red"
        android:layout_marginTop="10dp"
        android:text="Green LED" />
</RelativeLayout>


沒有留言:

張貼留言