{"id":6826,"date":"2015-02-17T17:16:18","date_gmt":"2015-02-17T08:16:18","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=6826"},"modified":"2015-02-17T19:17:58","modified_gmt":"2015-02-17T10:17:58","slug":"android-%e3%81%8b%e3%82%89-bluetoothrfcomm-%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e3%83%a2%e3%83%bc%e3%82%bf%e3%83%bc%e5%88%b6%e5%be%a1%e3%82%92%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/6826","title":{"rendered":"Android \u304b\u3089 Bluetooth+RFCOMM \u3092\u5229\u7528\u3057\u3066\u30e2\u30fc\u30bf\u30fc\u5236\u5fa1\u3092\u3059\u308b"},"content":{"rendered":"<p>Arduino \u3067 Bluetooth \u30b7\u30ea\u30a2\u30eb\u5909\u63db\u30e2\u30b8\u30e5\u30fc\u30eb(HC-05)\u3092\u4f7f\u3046 | Moonmile Solutions Blog<br \/>\n<a href=\"http:\/\/www.moonmile.net\/blog\/archives\/6819\">http:\/\/www.moonmile.net\/blog\/archives\/6819<\/a><\/p>\n<p>\u306e\u7d9a\u304d\u3067\u3059\u3002<br \/>\n\u30b9\u30c8\u30a2\u30a2\u30d7\u30ea\u3067\u4f5c\u308b\u3068\u3001\u30ce\u30fc\u30c8PC\u3067\u3061\u307e\u3061\u307e\uff08Surface\u3067\u3082\u3044\u3044\u3051\u3069\uff09\u3084\u3089\u306a\u3044\u3061\u3051\u306a\u3044\u306e\u3067\u3001\u30b9\u30de\u30fc\u30c8\u30d5\u30a9\u30f3\u304b\u3089\u52d5\u304b\u305b\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u3068\u8a00\u3044\u307e\u3059\u304b\u3001\u305b\u3063\u304b\u304f Xamarin.Android \u304c\u3042\u308b\u3093\u3060\u304b\u3089\u3001\u305d\u308c\u3067 RFCOMM \u3057\u3066\u3057\u307e\u304a\u3046\u3068\u3044\u3046\u8a33\u3067\u3059\u3002<\/p>\n<p>\u6700\u8fd1\u3001\u4e2d\u53e4\u3067\u8cfc\u5165\u3057\u305f Galaxy S3 \u306f\u3001Android 4.1.2 \u307e\u3067\u3057\u304b\u4e0a\u304c\u3089\u306a\u3044\u306e\u3067 BLE \u306f\u4f7f\u3048\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u5f93\u6765\u306e Bluetooth \u306f\u4f7f\u3048\u307e\u3059\u3002\u307e\u3042\u3001\u63a5\u7d9a\u5148\u304c Bluetooth 2.0 \u3067\u30b7\u30ea\u30a2\u30eb\u901a\u4fe1\u306a\u306e\u3067\u3053\u308c\u3067ok\u3002<\/p>\n<p>\u5185\u5bb9\u7684\u306b\u306f\u3001\u4ee5\u4e0b\u3092\u53c2\u8003\u306b\u3057\u3066\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>Connect to a Bluetooth Serial Device with Xamarin.Android<br \/>\n<a href=\"http:\/\/brianpeek.com\/post\/Connect-to-a-Bluetooth-Device-with-XamarinAndroid\">http:\/\/brianpeek.com\/post\/Connect-to-a-Bluetooth-Device-with-XamarinAndroid<\/a><\/p>\n<p>Android \u30a2\u30d7\u30ea\u306e\u753b\u9762\u306f\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/wpid-dworkblogimage20150217_03org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/wpid-dworkblogimage20150217_03thum.jpg\" border=\"0\" \/><\/a><\/p>\n<h2>Android \u3067 RFCOMM \u3092\u4f7f\u3046<\/h2>\n<p>Xamarin.Forms \u3092\u4f7f\u3046\u3068\u3001\u4f55\u6545\u304b Android.Bluetooth \u540d\u524d\u7a7a\u9593\u304c\u53c2\u7167\u3067\u304d\u306a\u3044\u306e\u3067\u3001\u30ce\u30fc\u30de\u30eb\u306a Xamarin.Forms \u3067\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\nBluetoothAdapter.DefaultAdapter \u3067\u30c7\u30d5\u30a9\u30eb\u30c8\u306e Bluetooth \u3092\u53d6\u3063\u3066\u304d\u3066\u3001CreateRfcommSocketToServiceRecord \u30e1\u30bd\u30c3\u30c9\u3067 RFCOMM \u7528\u306e\u30bd\u30b1\u30c3\u30c8\u3092\u4f5c\u308a\u307e\u3059\u3002\u30c7\u30fc\u30bf\u306e\u9001\u53d7\u4fe1\u306f\u3053\u308c\u306b\u5bfe\u3057\u3066\u3001OutputStream \u3068 InputStream \u3092\u4f7f\u3048\u3070 ok \u3067\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Activity(Label = &amp;quot;AndroidBluetooth&amp;quot;, MainLauncher = true, Icon = &amp;quot;@drawable\/icon&amp;quot;)]\r\npublic class MainActivity : Activity\r\n{\r\n\tTextView text1;\r\n\tEditText edit1;\r\n\tprotected override void OnCreate(Bundle bundle)\r\n\t{\r\n\t\tbase.OnCreate(bundle);\r\n\r\n\t\t\/\/ Set our view from the &amp;quot;main&amp;quot; layout resource\r\n\t\tSetContentView(Resource.Layout.Main);\r\n\r\n\t\t\/\/ Get our button from the layout resource,\r\n\t\t\/\/ and attach an event to it\r\n\t\tButton button = FindViewById&lt;Button&gt;(Resource.Id.MyButton);\r\n\t\tbutton.Click += button_Click;\r\n\t\tButton btnSend = FindViewById&lt;Button&gt;(Resource.Id.button1);\r\n\t\tbtnSend.Click += btnSend_Click;\r\n\t\ttext1 = FindViewById&lt;TextView&gt;(Resource.Id.textView1);\r\n\t\tedit1 = FindViewById&lt;EditText&gt;(Resource.Id.editText1);\r\n\t\tButton btn2 = FindViewById&lt;Button&gt;(Resource.Id.button2);\r\n\t\tbtn2.Click += btn2_Click;\r\n\t\tFindViewById&lt;Button&gt;(Resource.Id.button3).Click += clickMotorOn;\r\n\t\tFindViewById&lt;Button&gt;(Resource.Id.button4).Click += clickMotorOff;\r\n\t}\r\n\r\n\tBluetoothSocket _socket;\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u63a5\u7d9a\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\t\/\/\/ &lt;param name=&amp;quot;sender&amp;quot;&gt;&lt;\/param&gt;\r\n\t\/\/\/ &lt;param name=&amp;quot;e&amp;quot;&gt;&lt;\/param&gt;\r\n\tasync void button_Click(object sender, EventArgs e)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\r\n\t\t\tBluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;\r\n\t\t\tif (adapter == null)\r\n\t\t\t\tthrow new Exception(&amp;quot;No Bluetooth adapter found.&amp;quot;);\r\n\r\n\t\t\tif (!adapter.IsEnabled)\r\n\t\t\t\tthrow new Exception(&amp;quot;Bluetooth adapter is not enabled.&amp;quot;);\r\n\r\n\t\t\tBluetoothDevice device = (from bd in adapter.BondedDevices\r\n\t\t\t\t\t\t\t\t\t\twhere bd.Name == &amp;quot;HC-05&amp;quot;\r\n\t\t\t\t\t\t\t\t\t\tselect bd).FirstOrDefault();\r\n\t\t\tif (device == null)\r\n\t\t\t\tthrow new Exception(&amp;quot;Named device not found.&amp;quot;);\r\n\r\n\t\t\t_socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString(&amp;quot;00001101-0000-1000-8000-00805f9b34fb&amp;quot;));\r\n\t\t\tawait _socket.ConnectAsync();\r\n\t\t\ttext1.Text = &amp;quot;\u63a5\u7d9a\u3057\u307e\u3057\u305f&amp;quot;;\r\n\t\t}\r\n\t\tcatch (Exception ex)\r\n\t\t{\r\n\t\t\ttext1.Text = ex.Message;\r\n\t\t}\r\n\t}\r\n\r\n\tasync void SendCommand(string text)\r\n\t{\r\n\t\t\/\/ 8\u6587\u5b57\u306b\u3057\u3066\u9001\u308b\r\n\t\tif (text.Length &lt; 8)\r\n\t\t{\r\n\t\t\ttext = text.PadRight(8, &#039;*&#039;);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\ttext = text.Substring(0, 8);\r\n\t\t}\r\n\t\tvar buffer = System.Text.Encoding.UTF8.GetBytes(text);\r\n\t\t\/\/ \u9001\u4fe1\r\n\t\tawait _socket.OutputStream.WriteAsync(buffer, 0, buffer.Length);\r\n\t\t\/\/ \u53d7\u4fe1\u5f85\u3061\r\n\t\tvar buffer2 = new byte&#x5B;8];\r\n\t\tfor (int i = 0; i &lt; buffer2.Length; i++)\r\n\t\t{\r\n\t\t\tint n = _socket.InputStream.ReadByte();\r\n\t\t\tbuffer2&#x5B;i] = (byte)n;\r\n\t\t}\r\n\t\tstring str = System.Text.Encoding.UTF8.GetString(buffer2);\r\n\t\ttext1.Text = str;\r\n\t}\r\n\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u9001\u4fe1\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\t\/\/\/ &lt;param name=&amp;quot;sender&amp;quot;&gt;&lt;\/param&gt;\r\n\t\/\/\/ &lt;param name=&amp;quot;e&amp;quot;&gt;&lt;\/param&gt;\r\n\tvoid btnSend_Click(object sender, EventArgs e)\r\n\t{\r\n\t\tstring text = edit1.Text ;\r\n\t\tSendCommand(text);\r\n\t}\r\n\tvoid clickMotorOn(object sender, EventArgs e)\r\n\t{\r\n\t\tstring text = edit1.Text;\r\n\t\tSendCommand(&amp;quot;m1on&amp;quot;);\r\n\t}\r\n\tvoid clickMotorOff(object sender, EventArgs e)\r\n\t{\r\n\t\tstring text = edit1.Text;\r\n\t\tSendCommand(&amp;quot;m1off&amp;quot;);\r\n\t}\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ \u5207\u65ad\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\t\/\/\/ &lt;param name=&amp;quot;sender&amp;quot;&gt;&lt;\/param&gt;\r\n\t\/\/\/ &lt;param name=&amp;quot;e&amp;quot;&gt;&lt;\/param&gt;\r\n\tvoid btn2_Click(object sender, EventArgs e)\r\n\t{\r\n\t\t_socket.Close();\r\n\t\t_socket = null;\r\n\t\ttext1.Text = &amp;quot;\u5207\u65ad\u3057\u307e\u3057\u305f&amp;quot;;\r\n\t}\r\n}\r\n<\/pre>\n<p>\u30b3\u30de\u30f3\u30c9\u81ea\u4f53\u306f\u3001Arduino \u304c\u53d7\u3051\u3084\u3059\u3044\u3088\u3046\u306b8\u30d0\u30a4\u30c8\u56fa\u5b9a\u306b\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u5b9f\u9a13\u3057\u3066\u5206\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001Arduino \u304b\u3089\u306f1\u30d0\u30a4\u30c8\u305a\u3064\u9001\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u53d7\u4fe1\u3059\u308b Android \u3067 ReadAsync \u3092\u4f7f\u3046\u3068\u6700\u521d\u306e1\u30d0\u30a4\u30c8\u3060\u3051\u5148\u306b\u53d7\u4fe1\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u3053\u306e\u3042\u305f\u308a\u306f\u30d0\u30c3\u30d5\u30a1\u3092\u5148\u8aad\u307f\u3057\u30668\u30d0\u30a4\u30c8\u6e9c\u307e\u3063\u305f\u3089\u8aad\u307f\u8fbc\u3081\u3070\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u9762\u5012\u306e\u30671\u30d0\u30a4\u30c8\u305a\u3064\u8aad\u307f\u8fbc\u3093\u3067\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\t\t\/\/ \u53d7\u4fe1\u5f85\u3061\r\n\t\tvar buffer2 = new byte&#x5B;8];\r\n\t\tfor (int i = 0; i &lt; buffer2.Length; i++)\r\n\t\t{\r\n\t\t\tint n = _socket.InputStream.ReadByte();\r\n\t\t\tbuffer2&#x5B;i] = (byte)n;\r\n\t\t}\r\n<\/pre>\n<p>\u3042\u3068\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e Android Manifest \u3092\u958b\u3044\u3066 BLUETOOTH \u306b\u30c1\u30a7\u30c3\u30af\u3092\u5165\u308c\u307e\u3059\u3002<\/p>\n<p><a title=\"20150217_05\" href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/20150217_05.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/20150217_05.jpg\" alt=\"20150217_05\" width=\"566\" height=\"301\" class=\"alignnone  wp-image-6830\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/20150217_05.jpg 715w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/20150217_05-300x159.jpg 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u3053\u308c\u304c\u3046\u307e\u304f\u3044\u304f\u3068\u3001Android \u30b9\u30de\u30fc\u30c8\u30d5\u30a9\u30f3\u304b\u3089\u30e2\u30fc\u30bf\u30fc\u5236\u5fa1\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/wpid-dworkblogimage20150217_04org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/02\/wpid-dworkblogimage20150217_04thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u4e0a\u306e\u65b9\u306b\u5199\u3063\u3066\u3044\u308b\u767d\u3044\u30dc\u30fc\u30c9\u306f\u300cFreaduino UNO Rev1.8\u300d\u3067\u3059\u3002Arduino Uno \u306b\u30b5\u30fc\u30dc\u7528\u306e\u30b7\u30fc\u30eb\u30c9\u3092\u4f5c\u308b\u306e\u304c\u9762\u5012\u3067\u8cb7\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002\u624b\u5143\u306e meArm \u3092\u5236\u5fa1\u3057\u3066\u3044\u308b<\/p>\n<p>Assembly | Adafruit 16-Channel Servo Driver with Arduino | Adafruit Learning System<br \/>\n<a href=\"https:\/\/learn.adafruit.com\/16-channel-pwm-servo-driver\/assembly\">https:\/\/learn.adafruit.com\/16-channel-pwm-servo-driver\/assembly<\/a><\/p>\n<p>\u3068\u306f\u9055\u3046\u3051\u3069\u30c7\u30b8\u30bf\u30eb\u30d4\u30f3\u306b\uff11\u5bfe\uff11\u3067\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\u3067\u3001Arudino IDE \u3067 Servo \u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u305d\u306e\u307e\u307e\u4f7f\u3048\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arduino \u3067 Bluetooth \u30b7\u30ea\u30a2\u30eb\u5909\u63db\u30e2\u30b8\u30e5\u30fc\u30eb(HC-05)\u3092\u4f7f\u3046 | Moonmile Solutions Blog http:\/\/www.moonmile.net\/blog\/archives\/6819 &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/6826\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,64,71,62],"tags":[],"class_list":["post-6826","post","type-post","status-publish","format-standard","hentry","category-dev","category-android","category-arduino","category-xamarin"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/6826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=6826"}],"version-history":[{"count":4,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/6826\/revisions"}],"predecessor-version":[{"id":6832,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/6826\/revisions\/6832"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=6826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=6826"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=6826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}