ãã®äžé£ã®èšäºã«ã¯ãå¿ èŠãªéšåã®è³Œå ¥ããäžçã§ã®èµ·åãŸã§ãæåã®ããããããã°ããäœæããããã®æ å ±ãå«ãŸããŠããŸãã
ç§ã¯ææã®ç¬ç«ããç 究ãšç§ã®æåã®ããããã®äœæã®éçšã§èšäºãæžãããšã«æ³šæããŠãã ããã
èšäºãèªãã§ãã人ã¯ãã§ã«ä»¥äžã®çµéšãæã£ãŠãããšæ³å®ãããŸãã
- Androidããã€ã¹ã®éçºç°å¢ãã€ã³ã¹ããŒã«ããŠæ§æããŸãã
- Androidããã€ã¹çšã®æåã®Hello Worldããã°ã©ã ãäœæããŸãã
- Arduinoã®ãã©ã€ããŒãšéçºç°å¢ãã€ã³ã¹ããŒã«ããŸãã
- Arduinoã®æåã®ããã°ã©ã ïŒãªã³ããªãïŒLEDãäœæããŸãã
ç¥èª
BT-Bluetooth;
Android-ã¹ããŒããã©ã³ãªã©ãAndroid OSãæèŒããããã€ã¹ã
Sketch-Arduinoçšã®ã¢ããªã±ãŒã·ã§ã³ã
Arduino-Arduino Nanoã
ããããã®äžè¬çãªã¹ããŒã
ããããã«æ¬¡ã®äžè¬çãªã¹ããŒã ãæãããŸãïŒå³1ïŒã Androidã¯ããã¹ãŠã®ããŒã¿ãåŠçãããèšç®ãå®è¡ããããé è³ãã§ãã èšç®çµæã«åºã¥ããŠãBTãã£ãã«ã¯BTã¢ãžã¥ãŒã«ãåä¿¡ããã³ãã³ãã転éãããã®åŸArduinoã®ã·ãªã¢ã«ããŒãçµç±ã§åä¿¡ããŸãã Arduinoã¯åä¿¡ããã³ãã³ããåŠçãããçèãïŒã¢ãŒã¿ãŒã®LEDãªã©ïŒã䜿çšããŠå®è¡ããŸãã ããã«ãArduinoã¯ãæèŠåšå®ãïŒã»ã³ãµãŒãã»ã³ãµãŒãªã©ïŒããããŒã¿ãåä¿¡ããŸãããã®ããŒã¿ã¯äŸ¿å©ãªåœ¢åŒã«ãªããBTã¢ãžã¥ãŒã«ã䜿çšããŠãè³ãã«éä¿¡ãããŸãã ãããŠãããã¯ç¡éã«ç¹°ãè¿ãããŸãã
å³1
ãã®ãããªããããåè·¯ã®çè«çæ ¹æ ã¯æ¬¡ã®ãšããã§ãã
-BTã¢ãžã¥ãŒã«ãšArduino Nanoã¯ãäœã³ã¹ãã§ãäœç©ãšééãå°ããã
-Androidã¯ãé«äŸ¡ã§æé ãªäŸ¡æ Œã®ããã€ã¹ã§ã¯ãªããç§ãå«ããŠãã§ã«èšå€§ãªæ°ã®äººã ãããŸãã
-Androidã«ã¯ç¬èªã®ã»ã³ãµãŒãã»ã³ãµãŒãããã³æ å ±ã衚瀺ããç»é¢ããããŸã
-AndroidãšArduinoã®äž¡æ¹ã®ã¢ããªã±ãŒã·ã§ã³ã®ã³ãŒããç°¡åã«èšè¿°ã§ããŸãã
ãããããæ§ç¯ããã¿ã¹ã¯ãç°¡çŽ åããããã«ããåçŽãªãã®ããè€éãªãã®ãžãã¡ãœããã䜿çšããããšãææ¡ããŸãã ããã°ã©ãã³ã°èšèªã®ç 究ã®ããã«ãæåã®ããã°ã©ã ãHello wordããäœæããŸãã ãã¡ãããããã¯1ã€ã®ããã°ã©ã ã§ã¯ãªããå°ãªããšã2ã€ã§ãïŒarduinoãšandroidã®å ŽåïŒã 以éã®åèšäºã§ã¯ãããããã®æ©èœãåäžããŸãã
åé¡ã®å£°æ
以äžãåããã·ã³ãã«ãªããã€ã¹ãçµã¿ç«ãŠãŸãã
-2ãã¿ã³ïŒb1ãb2ïŒã
-1 LEDïŒLEDïŒã
ãããŠæ©èœãå®è¡ãããŸãïŒ
-ã¡ã€ã³ã¢ã¯ãã£ããã£ã«ã¯ããéä¿¡0ããšãéä¿¡1ãã®2ã€ã®ãã¿ã³ãå«ãŸããæŒããããšãããŒã¿ã¯ããããã0ããšã1ãã§BTãã£ã³ãã«ãä»ããŠã¢ã³ããã€ãããArduinoã«è»¢éãããArduinoã¯ããããåŠçããŠLEDãç¹ç¯ãŸãã¯æ¶ç¯ããŸãã
-Arduinoã§ãã¿ã³ãæŒãããæŸãããããããšã«é¢ããããŒã¿ãAndroidã«è»¢éãããããã«é¢ããæ å ±ãã¡ã€ã³ã¢ã¯ãã£ããã£ã®ç»é¢ã«è¡šç€ºãããŸãã
å¿ èŠãªéšåãšã¢ã»ã³ããªã®è³Œå ¥ã
1ïŒAndroidããã€ã¹-LG P500ã¹ããŒããã©ã³ïŒAndroidããŒãžã§ã³2.3.3ïŒããŸãã¯ããŒãžã§ã³2.3.3以éãæèŒãããã®ä»ã®Androidããã€ã¹ã åå¥ã«è³Œå ¥ããããã§ã¯ãªããããäŸ¡æ Œã衚瀺ããŸããããã¹ããŒããã©ã³ã䜿çšããŸãã
2ïŒArduino NanoV3.0ïŒ dx.com/en/p/nano-v3-0-avr-atmega328-p-20au-module-board-usb-cable-for-arduino-118037 ïŒ -11.32ãã«
3ïŒBluetoothã¢ãžã¥ãŒã«ïŒ dx.com/en/p/bluetooth-board-module-4-pin-121326ïŒ-$ 10.57
4ïŒ ãã¬ããããŒãã¯ã€ã€ïŒ dx.com/en/p/30cm-breadboard-wires-for-electronic-diy-40-cable-pack-80207ïŒ-$ 2.51
5ïŒãã¿ã³-2åãLED-1å -50ã«ãŒãã«
åèšïŒ855.20ã«ãŒãã«ã
ä»äºã«è¡ãã
Arduino
å©çšå¯èœãªéšåããåè·¯ãçµã¿ç«ãŠãŸãïŒå³2ïŒ
å³2
ã³ã³ãã¥ãŒã¿ãŒã«æ¥ç¶ããUSBââã±ãŒãã«ãä»ããŠarduinoã«é»åãäŸçµŠãããããã§å®è¡ãããŠãããœãããŠã§ã¢ïŒã¹ã±ããïŒãããŠã³ããŒããããŸãã Bluetoothã¢ãžã¥ãŒã«ããã®é»æºãåæãããŠããå ŽåïŒåºåïŒ17ïŒ3V3ïŒã«ã®ã¿ã¹ã±ãããarduinoã«ã¢ããããŒãã§ããããšã«æ³šæããŠãã ãããããããªããšãšã©ãŒãçºçããŸãã
Arduinoéçºç°å¢ãšãã©ã€ããŒã®ã€ã³ã¹ããŒã«ã®è©³çŽ°ã¯ãå ¬åŒWebãµã€ãïŒ arduino.ru/Guide/Windowsã«ãããŸãã
以äžã¯ãarduinoã«ããŒãããå¿ èŠãããã¹ã±ããã§ãã
// int led = 12; // int b1 = 11; // int b2 = 10; // int value_1,value_2 = 0; // char incomingbyte; // // void setup() { Serial.begin(38400); pinMode(led,OUTPUT); digitalWrite(led, HIGH); pinMode(b1,INPUT); digitalWrite(b1, HIGH); pinMode(b2,INPUT); digitalWrite(b2, HIGH); } // void contact_bounce(int buttton){ value_1 = digitalRead(buttton); if (!value_1){ delay(80); value_2 = digitalRead(buttton); if (!value_2){ Serial.print("Press button b"); Serial.println(buttton); } } } // void loop() { if (Serial.available() > 0){ incomingbyte = Serial.read(); if (incomingbyte == '1'){ digitalWrite(led,HIGH); Serial.println("LED ON"); } if (incomingbyte=='0'){ digitalWrite(led,LOW); Serial.println("LED OFF"); } } contact_bounce(b1); contact_bounce(b2); }
å€æ°ã宣èšããŸããåå€æ°ã«ã¯ã³ã¡ã³ãããããŸãã
ã·ãªã¢ã«æ¥ç¶ãéå§ããããŒã¿è»¢éã¬ãŒããããã/ cïŒããŒïŒã§èšå®ããŸãã BTã¢ãžã¥ãŒã«ã¯38400ã®é床ã§åäœããŸããã9600ã®é床ãå¯èœã§ãïŒBTã³ãã³ãã®é床ã¯ATã³ãã³ãã䜿çšããŠèšå®ã§ããŸãïŒã ç¹å®ã®å ¥å/åºåïŒãã³ïŒã®åäœã¢ãŒããå ¥åãŸãã¯åºåãšããŠèšå®ããŸãã å ¥åãšåºåã«é«ãå€ãäžããŸãã
void contact_bounceïŒint butttonïŒé¢æ°ã¯ããã¿ã³ã®æŒäžãåŠçãããã¿ã³ãªã©ã®æ©æ¢°çãªåãæ¿ãããã€ã¹ã§æ¥ç¹ãŸãã¯æ¥ç¹ãç¹°ãè¿ãééãããšãã«çºçããæ¥ç¹ããŠã³ã¹ãæé€ããŸãã
ã¡ã€ã³ã®ã«ãŒãã«ãŒãã§ã¯ãã·ãªã¢ã«ããŒãããªãã¹ã³ããããŒã¿ãå°çãããšãããããåŠçããŸãã ããã«ããã¿ã³ã®æŒäžãåŠçããã³ã³ã¿ã¯ãã®ããŠã³ã¹ãæé€ããæ©èœãåŒã³åºããŸãã
arduinoã«ã¹ã±ãããããŒãããããããŒãã¢ãã¿ãŒãå®è¡ããŠæäœæ§ã確èªã§ããŸãã ãã¿ã³ãæŒããšãããã¿ã³bãæŒããŠãã ããããšããã¡ãã»ãŒãžãã¢ãã¿ãŒãŠã£ã³ããŠã«è¡šç€ºãããŸãã Androidåãã®ã¢ããªã±ãŒã·ã§ã³ãäœæããåŸã«ã®ã¿ãLEDã®åäœãæ€èšŒããããšãã§ããŸãã
ãã®èšäºã§ã¯ããã®ã¹ã±ããã®ã¿ãarduinoã«äœ¿çšããŸãAndroidåãã¢ããªã±ãŒã·ã§ã³ã®éçºãå§ããŸãããã
Android
Androidã¢ããªã±ãŒã·ã§ã³ã®ãããã°ã®å©äŸ¿æ§ã®ããã«ããAndroidä»®æ³ããã€ã¹ãã§ã¯ãªããããããã°ãã¢ãŒãã§USBã±ãŒãã«ãä»ããŠã³ã³ãã¥ãŒã¿ãŒã«æ¥ç¶ãããAndroid OSããŒãžã§ã³2.3.3ã®å®éã®ã¹ããŒããã©ã³ã䜿çšããããšããå§ãããŸãã ãããè¡ãæ¹æ³ã«é¢ããèšå€§ãªæ°ã®èšäºããããŸãã
Androidã¹ããã1
æ°ãããããžã§ã¯ããAndroid application projectããäœæããŸã
BTã䜿çšããã«ã¯ãBTãã¢ããªã±ãŒã·ã§ã³ã§äœ¿çšããæš©å©ãèšå®ããå¿ èŠããããŸãã ãããè¡ãã«ã¯ããããã§ã¹ãã«ç§»åãã[ã¢ã¯ã»ã¹èš±å¯]ã¿ããéžæããŠ[è¿œå ]ãã¯ãªãã¯ãã[ã¢ã¯ã»ã¹èš±å¯ã䜿çš]ãã¯ãªãã¯ããŠã次ã®æš©éãèšå®ããŸããandroid.permission.BLUETOOTHãandroid.permission.BLUETOOTH_ADMIN
次ã«ãã¡ã€ã³ã¢ã¯ãã£ããã£ãäœæããres / layout / activity_main.xmlã«ã³ãŒããé 眮ããŸãã
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" "0"" /> <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" "1"" /> </LinearLayout> <TextView android:id="@+id/txtrobot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " /> </LinearLayout>
ãããã£ãŠãäž»ãªã¢ã¯ãã£ããã£ã¯æ¬¡ã®åœ¢åŒã«ãªããŸãã
å³3
txtrobotããã¹ãããã¯ã¹ã«ã¯ãå¿ èŠãªãã¹ãŠã®æ å ±ã衚瀺ãããŸãã ãã¿ã³b1ããã³b2ã¯ãarduinoã«ã³ãã³ããéä¿¡ããŸãã
ããã§src /../ MainActivity.javaã«ç§»åãããšãã¡ã€ã³ã³ãŒããé 眮ãããŸãã Bluetoothçšã®Apiããã±ãŒãžãæ¥ç¶ããŸãã
import android.bluetooth.*;
BTã䜿çšããåã«ãBTãAndroidã«ååšããããšã確èªããå¿ èŠããããŸãã BluetoothAdapterã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããŠã¿ãŸãããïŒAndroidã«ã€ã³ã¹ããŒã«ãããBTã¢ãžã¥ãŒã«ã®æäœãæ åœããŸãïŒïŒ
btAdapter = BluetoothAdapter.getDefaultAdapter();
Androidã«BTããªãå Žåãnullãè¿ãããŸãã
TextView mytext = (TextView) findViewById(R.id.txtrobot); if (btAdapter != null){ mytext.setText("Bluetooth "); }else { mytext.setText("Bluetooth "); }
ãã®ãã©ãŒã ã§ã¯ãããã°ã©ã ããã§ã«èµ·åã§ããŸãã Androidç»é¢ã«ãBluetoothãååšããŸãããšããç¢æã衚瀺ãããŸãã
ããã§ãBTããªã³ã«ãªã£ãŠããããšã確èªãããããªã³ã«ããããšãç³ãåºãå¿ èŠããããŸãã å®æ°ãè¿œå ããŸãã
private static final int REQUEST_ENABLE_BT = 1;
ããã³ã³ãŒãïŒ
if (btAdapter.isEnabled()){ mytext.setText("Bluetooth . ."); }else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
å€æŽãããã¢ããªã±ãŒã·ã§ã³ãAndroidã§èµ·åãããšããBluetoothããªã³ã«ããèš±å¯ã®ãªã¯ãšã¹ããã衚瀺ããã確èªãããŠBTãã¢ã¯ãã£ãã«ãªããŸãã
å®å šãªã¢ããªã±ãŒã·ã§ã³ã³ãŒãïŒ
package com.robot.bluetest; import android.os.Bundle; import android.app.Activity; import android.widget.TextView; import android.bluetooth.*; import android.content.Intent; public class MainActivity extends Activity { private static final int REQUEST_ENABLE_BT = 0; public BluetoothAdapter btAdapter; public TextView mytext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btAdapter = BluetoothAdapter.getDefaultAdapter(); mytext = (TextView) findViewById(R.id.txtrobot); if (btAdapter != null){ if (btAdapter.isEnabled()){ mytext.setText("Bluetooth . ."); }else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }else { mytext.setText("Bluetooth "); } } }
Androidã¹ããã2
ãããªãå®éšãè¡ãã«ã¯ãAndroidãšBTã¢ãžã¥ãŒã«ãããã¢ãªã³ã°ãããå¿ èŠããããŸãããã®ãããBTèšå®ã®Androidã§æ€çŽ¢ãå®è¡ããBTã¢ãžã¥ãŒã«ã«æ¥ç¶ããŸãããã¹ã¯ãŒãã¯ã1234ãã§ãã
çªç¶ãªã¢ãŒãBTã¢ãžã¥ãŒã«ãæ£åžžã«æ©èœããªãå Žåã¯ããã¹ãŠã®ã¯ã€ã€ïŒVCCãGNDãRXãTXïŒãåæããŠããŒããªã»ãããè¡ããããããåæ¥ç¶ããŠãã ãã-ããã圹ç«ã€ã¯ãã§ãã
次ã«ãããã°ã©ã ã§ãªã¢ãŒãBTã¢ãžã¥ãŒã«ã«æ¥ç¶ããŠã¿ãŸããããã¡ã€ã³æ¥ç¶ã³ãŒããonResumeã«é 眮ããŸãã onResumeã¯ã¢ã¯ãã£ããã£ã®ç¶æ ã®1ã€ã§ããã€ãŸããã¢ã¯ãã£ããã£ã¯ç»é¢ã«è¡šç€ºããããã©ãŒã«ã¹ãããããŠãŒã¶ãŒã¯ãããšå¯Ÿè©±ã§ããŸãã äž»ãªã¢ã¯ãã£ããã£ã³ãŒãã¯æ¬¡ã®ãšããã§ãã
package com.robot.bluetoothrob2; import java.io.IOException; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.bluetooth.*; import android.content.Intent; public class MainActivity extends Activity { private static final int REQUEST_ENABLE_BT = 0; final String LOG_TAG = "myLogs"; public BluetoothAdapter btAdapter; private BluetoothSocket btSocket = null; // MAC- Bluetooth private static String MacAdress = "20:11:02:47:01:60"; public TextView mytext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btAdapter = BluetoothAdapter.getDefaultAdapter(); mytext = (TextView) findViewById(R.id.txtrobot); if (btAdapter != null){ if (btAdapter.isEnabled()){ mytext.setText("Bluetooth . ."); }else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }else { MyError("Fatal Error", "Bluetooth "); } } @Override public void onResume() { super.onResume(); Log.d(LOG_TAG, "*** ***"); // MAC BluetoothDevice device = btAdapter.getRemoteDevice(MacAdress); mytext.setText("*** device = " + device.getName() + "***"); } private void MyError(String title, String message){ Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); finish(); } }
ãªã¢ãŒãBTã¢ãžã¥ãŒã«ãžã®ã¢ã¯ã»ã¹ã¯ãMACã¢ãã¬ã¹btAdapter.getRemoteDeviceïŒMacAdressïŒã«ãã£ãŠååŸãããŸãã Androidçšããã°ã©ã ïŒBluetooth Terminalã䜿çšããŠãBTã¢ãžã¥ãŒã«ã®MACã¢ãã¬ã¹ã確èªã§ããŸãã
BTã¢ãžã¥ãŒã«ãžã®ã¢ã¯ã»ã¹ã確å®ã«ååŸãããããã«ããã«ã¯ãgetNameïŒïŒã¡ãœããã䜿çšããŸããããã«ããããªã¢ãŒãBTã¢ãžã¥ãŒã«ã®ååãååŸããAndroidç»é¢ã«çµæã衚瀺ã§ããŸãã
ãŸãããã®äŸã§ã¯ããã°ãç¶æããæ©èœãè¿œå ãããŸãããããã¯ãããã°ã©ã ã®å®è¡äžã«è¡šç€ºãããæ¢åã®ãšã©ãŒãæ€åºã§ããŸãã ããã«ãã¢ããªã±ãŒã·ã§ã³ãã¯ã©ãã·ã¥ãããå¿ èŠãããå Žåã«åŒã³åºãããé¢æ°MyErrorãäœæãããŸããã
ã¢ããªã±ãŒã·ã§ã³ãèµ·åãããšããªã¢ãŒãBTã¢ãžã¥ãŒã«ã®ååãAndroidç»é¢ã®ãtxtrobotãããã¹ãããã¯ã¹ã«è¡šç€ºãããŸãã
Androidã¹ããã3
ãªã¢ãŒãBTã¢ãžã¥ãŒã«ãžã®ã¢ã¯ã»ã¹ãååŸãããŸããã次ã®ã¹ãããã¯ãAndroidããã¢ãžã¥ãŒã«ã«ããŒã¿ã転éããããšã§ãã onResumeïŒïŒã§ãããè¡ãã«ã¯ããœã±ãããäœæããŸãã
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
ããã§ãUUIDïŒUniversally Unique IdentifierïŒã¯ããœãããŠã§ã¢éçºã§äœ¿çšãããèå¥æšæºã§ãã UUIDå®æ°ãå®çŸ©ã«è¿œå ããŸãã
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
æ¥ç¶ãé ããªããªãããã«ãä»ã®BTããã€ã¹ã®æ€çŽ¢ããã£ã³ã»ã«ããŸãã
btAdapter.cancelDiscovery();
æ¥ç¶ããŠã¿ãŸãããïŒ
btSocket.connect();
æåããªãå Žåã¯ããœã±ãããéããŸãã
btSocket.close();
ããŒã¿ã®éåä¿¡æ¹æ³ã¯ãããã¯ããŠãããããã¡ã€ã³ã¢ããªã±ãŒã·ã§ã³ãããªãŒãºããªãããã«ãå¥ã®ã¹ã¬ããã§å®è¡ããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãã¯ã©ã¹ãäœæããŸãã
// private class ConnectedThred extends Thread{ private final BluetoothSocket copyBtSocket; private final OutputStream OutStrem; public ConnectedThred(BluetoothSocket socket){ copyBtSocket = socket; OutputStream tmpOut = null; try{ tmpOut = socket.getOutputStream(); } catch (IOException e){} OutStrem = tmpOut; } public void sendData(String message) { byte[] msgBuffer = message.getBytes(); Log.d(LOG_TAG, "*** : " + message + "***" ); try { OutStrem.write(msgBuffer); } catch (IOException e) {} } public void cancel(){ try { copyBtSocket.close(); }catch(IOException e){} } public Object status_OutStrem(){ if (OutStrem == null){return null; }else{return OutStrem;} } }
ãããªãã¯ConnectedThredïŒBluetoothSocketãœã±ããïŒã®ã³ã³ã¹ãã©ã¯ã¿ãŒã§ããœã±ãããä»ããããŒã¿ã®éä¿¡ãå¶åŸ¡ãããªããžã§ã¯ããäœæãããŸãã
tmpOut = socket.getOutputStream();
ã¡ã€ã³ã¢ã¯ãã£ããã£ããããŒã¿ãéä¿¡ããã«ã¯ãããã¹ãã¡ãã»ãŒãžãã©ã¡ãŒã¿ãŒã䜿çšããŠsendDataïŒæååã¡ãã»ãŒãžïŒã¡ãœãããåŒã³åºããŸãããã®ãã©ã¡ãŒã¿ãŒã¯ããã€ãåã«å€æãããŸãã cancelïŒïŒã¡ãœããã䜿çšãããšããœã±ãããéããããšãã§ããŸãã
sendDataïŒString messageïŒé¢æ°ãžã®åŒã³åºããå«ããã¿ã³b1ããã³b2ãæŒãããã®ãã³ãã©ãŒãäœæããããããã°ã«èšé²ããŸãã å®å šãªã¢ããªã±ãŒã·ã§ã³ã³ãŒãã以äžã«ç€ºããŸãã
package com.robot.bluetoothrob2; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.util.UUID; import com.robot.bluetoothrob2.R; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.bluetooth.*; import android.content.Intent; public class MainActivity extends Activity { private static final int REQUEST_ENABLE_BT = 1; final String LOG_TAG = "myLogs"; private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private static String MacAddress = "20:11:02:47:01:60"; // MAC- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private ConnectedThred MyThred = null; public TextView mytext; Button b1, b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btAdapter = BluetoothAdapter.getDefaultAdapter(); mytext = (TextView) findViewById(R.id.txtrobot); if (btAdapter != null){ if (btAdapter.isEnabled()){ mytext.setText("Bluetooth . ."); }else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }else { MyError("Fatal Error", "Bluetooth "); } b1 = (Button) findViewById(R.id.b1); b2 = (Button) findViewById(R.id.b2); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyThred.sendData("0"); mytext.setText(" : 0"); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyThred.sendData("1"); mytext.setText(" : 1"); } }); } @Override public void onResume() { super.onResume(); BluetoothDevice device = btAdapter.getRemoteDevice(MacAddress); Log.d(LOG_TAG, "*** Device***"+device.getName()); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); Log.d(LOG_TAG, "... ..."); } catch (IOException e) { MyError("Fatal Error", " onResume() : " + e.getMessage() + "."); } btAdapter.cancelDiscovery(); Log.d(LOG_TAG, "*** ***"); Log.d(LOG_TAG, "***...***"); try { btSocket.connect(); Log.d(LOG_TAG, "*** ***"); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { MyError("Fatal Error", " onResume() " + e2.getMessage() + "."); } } MyThred = new ConnectedThred(btSocket); } @Override public void onPause() { super.onPause(); Log.d(LOG_TAG, "...In onPause()..."); if (MyThred.status_OutStrem() != null) { MyThred.cancel(); } try { btSocket.close(); } catch (IOException e2) { MyError("Fatal Error", " onPause() " + e2.getMessage() + "."); } } private void MyError(String title, String message){ Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); finish(); } // private class ConnectedThred extends Thread{ private final BluetoothSocket copyBtSocket; private final OutputStream OutStrem; public ConnectedThred(BluetoothSocket socket){ copyBtSocket = socket; OutputStream tmpOut = null; try{ tmpOut = socket.getOutputStream(); } catch (IOException e){} OutStrem = tmpOut; } public void sendData(String message) { byte[] msgBuffer = message.getBytes(); Log.d(LOG_TAG, "*** : " + message + "***" ); try { OutStrem.write(msgBuffer); } catch (IOException e) {} } public void cancel(){ try { copyBtSocket.close(); }catch(IOException e){} } public Object status_OutStrem(){ if (OutStrem == null){return null; }else{return OutStrem;} } } }
äœæããã¢ããªã±ãŒã·ã§ã³ã§ã¯ãBTãä»ããŠAndroidããarduinoã«ããŒã¿ã転éã§ããŸããã0ããšã1ãã¯ãarduinoã®ã³ãã³ãã1ã-LEDãç¹ç¯ããã0ã-æ¶ç¯ããŸãã ãããã£ãŠãããããã®äžè¬çãªã¹ããŒã ãèæ ®ããŠããè³ããããçèãã«ã³ãã³ããäžããããšãåŠã³ãŸããã ãæèŠãããã®ããŒã¿ãåãå ¥ããããã«ã¢ã³ããã€ãã«æããããšã¯ãå°ããªå Žåã®ã±ãŒã¹ã§ãã
Androidã¹ããã4
ããŒã¿ã®åä¿¡ãšéä¿¡ã¯ãã¡ã€ã³ã¢ã¯ãã£ããã£ã®ãã³ã°ãåé¿ããããã«ãå¥ã®ã¹ã¬ããã§å®è¡ããå¿ èŠããããŸãã BTã¢ãžã¥ãŒã«ããåä¿¡ããããŒã¿ããã¡ã€ã³ã¢ã¯ãã£ããã£ã®ç»é¢ã®ããã¹ããã£ãŒã«ã-MyTextã«è¡šç€ºããŸãã ããããåé¡ãçºçããŸã-ãã¥ãŒã³ã³ããŒãã³ãã®æäœã¯ãã¡ã€ã³ã¹ããªãŒã ããã®ã¿å©çšå¯èœã§ãã ãããŠãç§ãã¡ãäœæããæ°ããã¹ã¬ããã¯ãç»é¢èŠçŽ ã«ã¢ã¯ã»ã¹ã§ããŸããã ãã®åé¡ã解決ããã«ã¯ããã³ãã©ãŒã¡ã«ããºã ã䜿çšããŸãã
ãã³ãã©ãŒã¯ãã¡ãã»ãŒãžãã¥ãŒãæäœã§ããã¡ã«ããºã ã§ãã ç¹å®ã®ã¹ã¬ããïŒã¹ã¬ããïŒã«æ¥ç¶ããããã®ãã¥ãŒã§åäœããŸãã ãã³ãã©ãŒã¯ã¡ãã»ãŒãžããã¥ãŒã«å ¥ããããšãã§ããŸãã åæã«ã圌ã¯ãã®ã¡ãã»ãŒãžã®åä¿¡è ãšããŠèªåèªèº«ã眮ããŸãã ãããŠãæéãæ¥ããšãã·ã¹ãã ã¯ã¡ãã»ãŒãžããã¥ãŒããåé€ããåŠçã®ããã«åä¿¡è ïŒã€ãŸãããã³ãã©ãŒå ïŒã«éä¿¡ããŸãã
ãã³ãã©ãŒã®å®£èšïŒ
Handler h;
ãã³ãã©ãŒãäœæããŸãã
h = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case ArduinoData: byte[] readBuf = (byte[]) msg.obj; String strIncom = new String(readBuf, 0, msg.arg1); mytext.setText(" Arduino: " + strIncom); break; } }; };
handleMessageã¡ãã»ãŒãžåŠçã¡ãœãããå®è£ ããŸãã ã¡ãã»ãŒãžããwhatãobjå±æ§ãããã³intåã®åŒæ°ãæœåºããŸãã åä¿¡ããã¡ãã»ãŒãžãæååã«å€æããã¡ã€ã³ã¢ã¯ãã£ããã£ã®ããã¹ããã£ãŒã«ãã«è¡šç€ºããŸããmytext.setTextïŒ "Arduinoã®ããŒã¿ïŒ" + strIncomïŒ;
ããŒã¿è»¢éçšã®ã¹ããªãŒã ã«ããã®ã¹ããªãŒã ãéå§ããé¢æ°ãè¿œå ããããã«ããŒã¿ãèªã¿åãããã®é¢æ°ã§ãµã€ã¯ã«ãé 眮ããŸãã
public void run() { byte[] buffer = new byte[1024]; int bytes; while(true){ try{ bytes = InStrem.read(buffer); h.obtainMessage(ArduinoData, bytes, -1, buffer).sendToTarget(); }catch(IOException e){break;} } }
å®å šãªã¢ããªã±ãŒã·ã§ã³ã³ãŒãã以äžã«ç€ºããŸãã
package com.robot.bluetoothrob2; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.UUID; import com.robot.bluetoothrob2.R; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.bluetooth.*; import android.content.Intent; public class MainActivity extends Activity { private static final int REQUEST_ENABLE_BT = 1; final int ArduinoData = 1; final String LOG_TAG = "myLogs"; private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private static String MacAddress = "20:11:02:47:01:60"; // MAC- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private ConnectedThred MyThred = null; public TextView mytext; Button b1, b2; Handler h; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btAdapter = BluetoothAdapter.getDefaultAdapter(); mytext = (TextView) findViewById(R.id.txtrobot); if (btAdapter != null){ if (btAdapter.isEnabled()){ mytext.setText("Bluetooth . ."); }else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } }else { MyError("Fatal Error", "Bluetooth "); } b1 = (Button) findViewById(R.id.b1); b2 = (Button) findViewById(R.id.b2); b1.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyThred.sendData("0"); //mytext.setText(" : 0"); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyThred.sendData("1"); // mytext.setText(" : 1"); } }); h = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case ArduinoData: byte[] readBuf = (byte[]) msg.obj; String strIncom = new String(readBuf, 0, msg.arg1); mytext.setText(" Arduino: " + strIncom); break; } }; }; } @Override public void onResume() { super.onResume(); BluetoothDevice device = btAdapter.getRemoteDevice(MacAddress); Log.d(LOG_TAG, "*** Device***"+device.getName()); try { btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); Log.d(LOG_TAG, "... ..."); } catch (IOException e) { MyError("Fatal Error", " onResume() : " + e.getMessage() + "."); } btAdapter.cancelDiscovery(); Log.d(LOG_TAG, "*** ***"); Log.d(LOG_TAG, "***...***"); try { btSocket.connect(); Log.d(LOG_TAG, "*** ***"); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { MyError("Fatal Error", " onResume() " + e2.getMessage() + "."); } } MyThred = new ConnectedThred(btSocket); MyThred.start(); } @Override public void onPause() { super.onPause(); Log.d(LOG_TAG, "...In onPause()..."); if (MyThred.status_OutStrem() != null) { MyThred.cancel(); } try { btSocket.close(); } catch (IOException e2) { MyError("Fatal Error", " onPause() " + e2.getMessage() + "."); } } private void MyError(String title, String message){ Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); finish(); } // private class ConnectedThred extends Thread{ private final BluetoothSocket copyBtSocket; private final OutputStream OutStrem; private final InputStream InStrem; public ConnectedThred(BluetoothSocket socket){ copyBtSocket = socket; OutputStream tmpOut = null; InputStream tmpIn = null; try{ tmpOut = socket.getOutputStream(); tmpIn = socket.getInputStream(); } catch (IOException e){} OutStrem = tmpOut; InStrem = tmpIn; } public void run() { byte[] buffer = new byte[1024]; int bytes; while(true){ try{ bytes = InStrem.read(buffer); h.obtainMessage(ArduinoData, bytes, -1, buffer).sendToTarget(); }catch(IOException e){break;} } } public void sendData(String message) { byte[] msgBuffer = message.getBytes(); Log.d(LOG_TAG, "*** : " + message + "***" ); try { OutStrem.write(msgBuffer); } catch (IOException e) {} } public void cancel(){ try { copyBtSocket.close(); }catch(IOException e){} } public Object status_OutStrem(){ if (OutStrem == null){return null; }else{return OutStrem;} } } }
ã¹ããã4ã§ã¯ãAndroidããarduinoã«BTã³ãã³ããéä¿¡ããarduinoããããŒã¿ãåä¿¡ã§ããããã«ããã¢ããªã±ãŒã·ã§ã³ã衚瀺ãããŸãã ãããã£ãŠãã³ãã³ããäžããŠããŒã¿ãåä¿¡ããããšãåŠã³ãã¿ã¹ã¯ãå®äºããŸããã
ããã«ããã®ã¢ããªã±ãŒã·ã§ã³ã¯ãã³ãã¬ãŒããªã®ã§ãããè€éãªã¢ããªã±ãŒã·ã§ã³ãäœæã§ããŸããããã«ãããããšãã°è¶ é³æ³¢ã»ã³ãµãŒããã®ããŒã¿ãåŠçããããã¢ãŒã¿ãŒã«ã³ãã³ããäžããŠãããããåããããã§ããŸãã
次ã®èšäºã§ã¯ãæ°ããéšåãšã¢ãžã¥ãŒã«ã泚æããŸããã 以äžã¯ãªã¹ããšäŸ¡æ Œã§ãã
ãåå | ãªã³ã¯ | ããªãããã®äŸ¡æ Œ | äŸ¡æ Œæ©æŠ | æ°é | éé¡ |
ã¬ã€ã¢ãŠã | dx.com/p/solderless-breadboard-with-400-tie-point-white-121534 | 3,1 | 102.3 | 2 | 204.6 |
è¶ é³æ³¢ã»ã³ãµãŒ | dx.com/p/ultrasonic-sensor-distance-measuring-module-138563 | 3.9 | 128.7 | 3 | 386.1 |
ã¢ãŒã¿ãŒçšãã©ã€ããŒ2åã | dx.com/en/p/hg7881-two-channel-motor-driver-board-blue-green-black-2-5-12v-215795 | 2,8 | 92.4 | 2 | 184.8 |
ãã©ãããã©ãŒã | dx.com/en/p/zl-4-smart-car-chassis-kit-for-arduino-black-yellow-152992 | 28,2 | 930.6 | 1 | 930.6 |
æ¥ç¶é ç· | dx.com/p/breadboard-jumper-wire-cord-kit-for-arduino-diy-140-piece-pack-138220 | 6.9 | 227.7 | 1 | 227.7 |
æ é€ | dx.com/en/p/dc-power-converter-module-for-electronic-diy-219232 | 2,3 | 75.9 | 1 | 75.9 |
åèšïŒ1933.8