Android手机控制ZigBee板上LED
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android手机控制ZigBee板上LED
分类:ZigBeeAndroid2013-08-06 16:39344人阅读评论(0)收藏举报环境:Windows
编译器:IAREW8051-8.1
硬件:CC2530
协议栈:ZStack-CC2530-2.3.0-1.4.0
手机:Android4.1.2
又重新开始学习ZigBee技术,摸索了一哈通信流程,本例程没有涉及到ZigBee组网,读者可自行扩展。
ZigBee流程如下:从串口接收指令信息,根据指令信息控制相应的LED资源,工作模式为协调器,可用GenericApp工程改编,代码如下
[cpp]view plaincopyprint?
1./***********************************************************************************
***************
2.Filename: TESTApp.c
3.Revised: $Date: 2009-03-18 15:56:27 -0700 (Wed, 18 Mar 2009) $
4.Revision: $Revision: 19453 $
5.
6.Description: TEST Application (no Profile).
7.
8.
9.Copyright 2004-2009 Texas Instruments Incorporated. All rights reserved.
10.
11.IMPORTANT: Your use of this Software is limited to those specific rights
12.granted under the terms of a software license agreement between the user
13.who downloaded the software, his/her employer (which must be your employer)
14.and Texas Instruments Incorporated (the "License"). You may not use this
15.Software unless you agree to abide by the terms of the License. The License
16.limits your use, and you acknowledge, that the Software may not be modified,
17.copied or distributed unless embedded on a Texas Instruments microcontroller
18.or used solely and exclusively in conjunction with a Texas Instruments radio
19.frequency transceiver, which is integrated into your product. Other than for
20.the foregoing purpose, you may not use, reproduce, copy, prepare derivative
21.works of, modify, distribute, perform, display or sell this Software and/or
22.its documentation for any purpose.
23.
24.YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
25.PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
26.INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
27.NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
28.TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
29.NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
30.LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
31.INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
32.OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
33.OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
34.(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
35.
36.Should you have any questions regarding your right to use this Software,
37.contact Texas Instruments Incorporated at .
38.***********************************************************************************
***************/
39.
40./*********************************************************************
41.This application isn't intended to do anything useful, it is
42.intended to be a simple example of an application's structure.
43.
44.This application sends "Hello World" to another "TEST"
45.application every 15 seconds. The application will also
46.receive "Hello World" packets.
47.
48.The "Hello World" messages are sent/received as MSG type message.
49.
50.This applications doesn't have a profile, so it handles everything
51.directly - itself.
52.
53.Key control:
54.SW1:
55.SW2: initiates end device binding
56.SW3:
57.SW4: initiates a match description request
58.*********************************************************************/
59.
60./*********************************************************************
61.* INCLUDES
62.*/
63.#include "OSAL.h"
64.#include "AF.h"
65.#include "ZDApp.h"
66.#include "ZDObject.h"
67.#include "ZDProfile.h"
68.
69.#include "Common.h"
70.#include "DebugTrace.h"
71.
72.#if !defined( WIN32 )
73.#include "OnBoard.h"
74.#endif
75.
76./* HAL */
77.#include "hal_lcd.h"
78.#include "hal_led.h"
79.#include "hal_key.h"
80.#include "hal_uart.h"
81.
82.#include "OSAL_Nv.h"
83.
84./*********************************************************************
85.* MACROS
86.*/
87.
88./*********************************************************************
89.* CONSTANTS
90.*/
91.
92./*********************************************************************
93.* TYPEDEFS
94.*/
95.
96./*********************************************************************
97.* GLOBAL VARIABLES
98.*/
99.
100.// This list should be filled with Application specific Cluster IDs. 101.c onst cId_t TESTApp_ClusterList[TESTAPP_MAX_CLUSTERS] =
102.{
103.T ESTAPP_CLUSTERID
104.};
105.
106.c onst SimpleDescriptionFormat_t TestApp_SimpleDesc =
107.{
108.T ESTAPP_ENDPOINT, // int Endpoint;
109.T ESTAPP_PROFID, // uint16 AppProfId[2];
110.T ESTAPP_DEVICEID, // uint16 AppDeviceId[2];
111.T ESTAPP_DEVICE_VERSION, // int AppDevVer:4;
112.T ESTAPP_FLAGS, // int AppFlags:4;
113.T ESTAPP_MAX_CLUSTERS, // byte AppNumInClusters;
114.(cId_t *)TESTApp_ClusterList, // byte *pAppInClusterList;
115.0, // byte AppNumInClusters;
116.(cId_t *)NULL // byte *pAppInClusterList;
117.};//用于描述一个设备节点
118.
119.// This is the Endpoint/Interface description. It is defined here, but 120.// filled-in in TESTApp_Init(). Another way to go would be to fill 121.// in the structure here and make it a "const" (in code space). The 122.// way it's defined in this sample app it is define in RAM.
123.e ndPointDesc_t TestApp_epDesc;//节点描述符
124.
125./********************************************************************* 126.* EXTERNAL VARIABLES
127.*/
128.
129./********************************************************************* 130.* EXTERNAL FUNCTIONS
131.*/
132.
133./********************************************************************* 134.* LOCAL VARIABLES
135.*/
136.b yte TestApp_TaskID; // Task ID for internal task/event processing 137.// This variable will be received when
138.// TESTApp_Init() is called.
139.d evStates_t TestApp_NwkState;
140.
141.
142.b yte TestApp_TransID; // This is the unique message ID (counter) 143.
144.a fAddrType_t TestApp_DstAddr;
145.
146.
147.s tatic uint8 SerialApp_TxLen;
148.s tatic void rxCB(uint8 port, uint8 event);
149./********************************************************************* 150.* LOCAL FUNCTIONS
151.*/
152.v oid TestApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
153.v oid TestApp_HandleKeys( byte shift, byte keys );
154.v oid TestApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
155.v oid TestApp_SendTheMessage( void );
156.
157./********************************************************************* 158.* NETWORK LAYER CALLBACKS
159.*/
160.
161./********************************************************************* 162.* PUBLIC FUNCTIONS
163.*/
164.
165./********************************************************************* 166.* @fn TESTApp_Init
167.*
168.* @brief Initialization function for the TEST App Task.
169.* This is called during initialization and should contain
170.* any application specific initialization (ie. hardware
171.* initialization/setup, table initialization, power up
172.* notificaiton ... ).
173.*
174.* @param task_id - the ID assigned by OSAL. This ID should be
175.* used to send messages and set timers.
176.*
177.* @return none
178.*/
179.v oid TestApp_Init( byte task_id )
180.{
181.h alUARTCfg_t uartConfig;
182.T estApp_TaskID = task_id;
183.T estApp_NwkState = DEV_INIT;
184.T estApp_TransID = 0;
185.
186.// Device hardware initialization can be added here or in main() (Zmain.c). 187.// If the hardware is application specific - add it here.
188.// If the hardware is other parts of the device add it in main().
189.
190.T estApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
191.T estApp_DstAddr.endPoint = TESTAPP_ENDPOINT;
192.T estApp_DstAddr.addr.shortAddr = 0xFFFF;
193.
194.// Fill out the endpoint description.
195.T estApp_epDesc.endPoint = TESTAPP_ENDPOINT;
196.T estApp_epDesc.task_id = &TestApp_TaskID;
197.T estApp_epDesc.simpleDesc
198.= (SimpleDescriptionFormat_t *)&TestApp_SimpleDesc;
199.T estApp_tencyReq = noLatencyReqs;
200.
201.// Register the endpoint description with the AF
202.a fRegister( &TestApp_epDesc );
203.
204.u artConfig.configured = TRUE;
205.u artConfig.baudRate = HAL_UART_BR_115200;
206.u artConfig.flowControl = FALSE;
207.u artConfig.flowControlThreshold = 64; // 2x30 don't care - see uart driver. 208.u artConfig.rx.maxBufSize = 128; // 2x30 don't care - see uart driver. 209.u artConfig.tx.maxBufSize = 128; // 2x30 don't care - see uart driver. 210.u artConfig.idleTimeout = 6; // 2x30 don't care - see uart driver.
211.u artConfig.intEnable = TRUE; // 2x30 don't care - see uart driver.
212.u artConfig.callBackFunc = rxCB;
213.H alUARTOpen (0, &uartConfig);
214.
215.// Register for all key events - This app will handle all key events
216.//RegisterForKeys( TestApp_TaskID );
217.
218.// Update the display
219.#if defined ( LCD_SUPPORTED )
220.H alLcdWriteString( "TestApp", HAL_LCD_LINE_1 );
221.#endif
222.
223.Z DO_RegisterForZDOMsg( TestApp_TaskID, End_Device_Bind_rsp );
224.Z DO_RegisterForZDOMsg( TestApp_TaskID, Match_Desc_rsp );
225.}
226.
227./*********************************************************************
228.* @fn TESTApp_ProcessEvent
229.*
230.* @brief TEST Application Task event processor. This function
231.* is called to process all events for the task. Events
232.* include timers, messages and any other user defined events.
233.*
234.* @param task_id - The OSAL assigned task ID.
235.* @param events - events to process. This is a bit map and can
236.* contain more than one event.
237.*
238.* @return none
239.*/
240.U INT16 TestApp_ProcessEvent( byte task_id, UINT16 events )
241.{
242.a fIncomingMSGPacket_t *MSGpkt;
243.
244.
245.
246.(void)task_id; // Intentionally unreferenced parameter
247.
248.i f ( events & SYS_EVENT_MSG )
249.{
250.M SGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TestApp_TaskID );
251.w hile ( MSGpkt )
252.{
253.s witch ( MSGpkt->hdr.event )
254.{
255.
256.c ase KEY_CHANGE:
257.T estApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); 258.b reak;
260.c ase AF_INCOMING_MSG_CMD:
261.T estApp_MessageMSGCB( MSGpkt );
262.b reak;
263.
264.d efault:
265.b reak;
266.}
267.
268.// Release the memory
269.o sal_msg_deallocate( (uint8 *)MSGpkt );//释放消息所占用空间
270.
271.// Next
272.M SGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( TestApp_TaskID );//接着获取消息273.}
274.
275.// return unprocessed events
276.r eturn (events ^ SYS_EVENT_MSG);
277.}
278.
279.// Send a message out - This event is generated by a timer
280.// (setup in TESTApp_Init()).
281.i f ( events & TESTAPP_SEND_MSG_EVT )
282.{
283.// Send "the" message
284.T estApp_SendTheMessage();
285.
286.// Setup to send message again
287.o sal_start_timerEx( TestApp_TaskID,
288.T ESTAPP_SEND_MSG_EVT,
289.T ESTAPP_SEND_MSG_TIMEOUT );
290.
291.// return unprocessed events
292.r eturn (events ^ TESTAPP_SEND_MSG_EVT);
293.}
294.
295.// Discard unknown events
296.r eturn 0;
297.}
299./********************************************************************* 300.* Event Generation Functions
301.*/
302.
303./********************************************************************* 304.* @fn TESTApp_ProcessZDOMsgs()
305.*
306.* @brief Process response messages
307.*
308.* @param none
309.*
310.* @return none
311.*/
312.v oid TestApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
313.{
314.s witch ( inMsg->clusterID )
315.{
316.c ase End_Device_Bind_rsp:
317.i f ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
318.{
319.// Light LED
320.H alLedSet( HAL_LED_4, HAL_LED_MODE_ON );
321.}
322.#if defined(BLINK_LEDS)
323.e lse
324.{
325.// Flash LED to show failure
326.H alLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH );
327.}
328.#endif
329.b reak;
330.
331.c ase Match_Desc_rsp:
332.{
333.Z DO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg );
334.i f ( pRsp )
335.{
336.i f ( pRsp->status == ZSuccess && pRsp->cnt )
338.T estApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
339.T estApp_DstAddr.addr.shortAddr = pRsp->nwkAddr;
340.// Take the first endpoint, Can be changed to search through endpoints 341.T estApp_DstAddr.endPoint = pRsp->epList[0];
342.
343.// Light LED
344.H alLedSet( HAL_LED_4, HAL_LED_MODE_ON );
345.}
346.o sal_mem_free( pRsp );
347.}
348.}
349.b reak;
350.}
351.}
352.
353./********************************************************************* 354.* @fn TESTApp_HandleKeys
355.*
356.* @brief Handles all key events for this device.
357.*
358.* @param shift - true if in shift/alt.
359.* @param keys - bit field for key events. Valid entries:
360.* HAL_KEY_SW_4
361.* HAL_KEY_SW_3
362.* HAL_KEY_SW_2
363.* HAL_KEY_SW_1
364.*
365.* @return none
366.*/
367.v oid TestApp_HandleKeys( byte shift, byte keys )
368.{
369.z AddrType_t dstAddr;
370.
371.// Shift is used to make each button/switch dual purpose.
372.i f ( shift )
373.{
374.i f ( keys & HAL_KEY_SW_1 )
375.{
376.}
377.i f ( keys & HAL_KEY_SW_2 )
378.{
379.}
380.i f ( keys & HAL_KEY_SW_3 )
381.{
382.}
383.i f ( keys & HAL_KEY_SW_4 )
384.{
385.}
386.}
387.e lse
388.{
389.i f ( keys & HAL_KEY_SW_1 )
390.{
391.}
392.
393.i f ( keys & HAL_KEY_SW_2 )
394.{
395.H alLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
396.
397.// Initiate an End Device Bind Request for the mandatory endpoint 398.d stAddr.addrMode = Addr16Bit;
399.d stAddr.addr.shortAddr = 0x0000; // Coordinator
400.Z DP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
401.T estApp_epDesc.endPoint,
402.T ESTAPP_PROFID,
403.T ESTAPP_MAX_CLUSTERS, (cId_t *)TESTApp_ClusterList,
404.T ESTAPP_MAX_CLUSTERS, (cId_t *)TESTApp_ClusterList,
405.F ALSE );
406.}
407.
408.i f ( keys & HAL_KEY_SW_3 )
409.{
410.}
411.
412.i f ( keys & HAL_KEY_SW_4 )
413.{
414.H alLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
415.// Initiate a Match Description Request (Service Discovery)
416.d stAddr.addrMode = AddrBroadcast;
417.d stAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
418.Z DP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
419.T ESTAPP_PROFID,
420.T ESTAPP_MAX_CLUSTERS, (cId_t *)TESTApp_ClusterList,
421.T ESTAPP_MAX_CLUSTERS, (cId_t *)TESTApp_ClusterList,
422.F ALSE );
423.}
424.}
425.}
426.
427./********************************************************************* 428.* LOCAL FUNCTIONS
429.*/
430.
431./********************************************************************* 432.* @fn TESTApp_MessageMSGCB
433.*
434.* @brief Data message processor callback. This function processes 435.* any incoming data - probably from other devices. So, based
436.* on cluster ID, perform the intended action.
437.*
438.* @param none
439.*
440.* @return none
441.*/
442.v oid TestApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
443.{
444.u nsigned char buffer;
445.s tatic unsigned char LEDState[5];
446.s witch ( pkt->clusterId )
447.{
448.c ase TESTAPP_CLUSTERID:
449.// "the" message
450.o sal_memcpy(&buffer, pkt->cmd.Data, 1);
451.s witch(buffer)
452.{
453.c ase 1:
454.L EDState[0]++;
455.i f(LEDState[0] == 2)
456.{
457.L EDState[0] = 0;
458.}
459.i f(LEDState[0] == 0)
460.{
461.H alLedSet(HAL_LED_1, HAL_LED_MODE_ON); 462.}
463.e lse
464.{
465.H alLedSet(HAL_LED_1, HAL_LED_MODE_OFF); 466.}
467.b reak;
468.c ase 2:
469.L EDState[1]++;
470.i f(LEDState[1] == 2)
471.{
472.L EDState[1] = 0;
473.}
474.i f(LEDState[1] == 0)
475.{
476.H alLedSet(HAL_LED_2, HAL_LED_MODE_ON); 477.}
478.e lse
479.{
480.H alLedSet(HAL_LED_2, HAL_LED_MODE_OFF); 481.}
482.b reak;
483.c ase 3:
484.L EDState[2]++;
485.i f(LEDState[2] == 2)
486.{
487.L EDState[2] = 0;
488.}
489.i f(LEDState[2] == 0)
490.{
491.H alLedSet(HAL_LED_3, HAL_LED_MODE_ON); 492.}
493.e lse
494.{
495.H alLedSet(HAL_LED_3, HAL_LED_MODE_OFF);
496.}
497.b reak;
498.c ase 4:
499.L EDState[3]++;
500.i f(LEDState[3] == 2)
501.{
502.L EDState[3] = 0;
503.}
504.i f(LEDState[3] == 0)
505.{
506.H alLedSet(HAL_LED_4, HAL_LED_MODE_ON);
507.}
508.e lse
509.{
510.H alLedSet(HAL_LED_4, HAL_LED_MODE_OFF);
511.}
512.b reak;
513.c ase 5:
514.L EDState[4]++;
515.i f(LEDState[4] == 2)
516.{
517.L EDState[4] = 0;
518.}
519.i f(LEDState[4] == 0)
520.{
521.H alLedSet(HAL_LED_ALL, HAL_LED_MODE_ON);
522.}
523.e lse
524.{
525.H alLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF);
526.}
527.b reak;
528.}
529.#if defined( LCD_SUPPORTED )
530.H alLcdWriteScreen( (char*)pkt->cmd.Data, "rcvd" ); 531.#elif defined( WIN32 )
532.W PRINTSTR( pkt->cmd.Data );
533.#endif
534.b reak;
535.}
536.}
537.
538./********************************************************************* 539.* @fn TESTApp_SendTheMessage
540.*
541.* @brief Send "the" message.
542.*
543.* @param none
544.*
545.* @return none
546.*/
547.v oid TestApp_SendTheMessage( void )
548.{
549.c har theMessageData[] = "LED";
550.
551.i f ( AF_DataRequest( &TestApp_DstAddr, &TestApp_epDesc,
552.T ESTAPP_CLUSTERID,
553.3,
554.t heMessageData,
555.&TestApp_TransID,
556.A F_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
557.{
558.// Successfully requested to be sent.
559.}
560.e lse
561.{
562.// Error occurred in request to send.
563.}
564.}
565.
566./********************************************************************* 567.*********************************************************************/ 568.s tatic void rxCB(uint8 port, uint8 event)
569.{
570.u int8 Uartbuf[80];
571.u int8 buffer;
572.s tatic unsigned char LEDState[5];
573.b ool result = false;
574.i f((event &(HAL_UART_RX_FULL|HAL_UART_RX_ABOUT_FULL|HAL_UART_RX_TIMEOUT)) && 575.#if SERIAL_APP_LOOPBACK
576.(SerialApp_TxLen < SERIAL_APP_TX_MAX))
577.#else
578.!SerialApp_TxLen)
579.#endif
580.{
581.S erialApp_TxLen = HalUARTRead(0, Uartbuf, 2);
582.
583.o sal_memcpy(&buffer, Uartbuf, 1);
584.b uffer -=0x30;
585.s witch(buffer)
586.{
587.c ase 0:
588.H alLedSet(HAL_LED_1, HAL_LED_MODE_ON);
589.r esult = true;
590.b reak;
591.c ase 1:
592.H alLedSet(HAL_LED_2, HAL_LED_MODE_ON);
593.r esult = true;
594.b reak;
595.c ase 2:
596.H alLedSet(HAL_LED_3, HAL_LED_MODE_ON);
597.r esult = true;
598.b reak;
599.c ase 3:
600.H alLedSet(HAL_LED_ALL, HAL_LED_MODE_ON);
601.r esult = true;
602.b reak;
603.c ase 4:
604.H alLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
605.r esult = true;
606.b reak;
607.c ase 5:
608.H alLedSet(HAL_LED_2, HAL_LED_MODE_OFF);
609.r esult = true;
610.b reak;
611.c ase 6:
612.H alLedSet(HAL_LED_3, HAL_LED_MODE_OFF);
613.r esult = true;
614.b reak;
615.c ase 7:
616.H alLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF);
617.r esult = true;
618.b reak;
619.}
620.i f(result)
621.{
622.H alUARTWrite(0, "success\r\n", 9);
623.}
624.e lse
625.{
626.H alUARTWrite(0, "failed\r\n", 8);
627.}
628.S erialApp_TxLen = 0;
629.}
630.}
只需查看TestApp_Init()以及rxCB()函数即可,在TestApp_Init()函数中对串口0进行配置,在rxCB()函数中对接收到的数据进行解析并对LED资源做出相应的处理。
下载程序到板子上,此时可用串口工具来实现对LED资源的控制,串口信息如下所示:
此时已经完成了一半。
Android端流程:Android建立一个TCP的服务端,然后等待连接,有连接接入时即可发送控制指令到TCP的客户端,
布局文件如下
[html]view plaincopyprint?
1.<?xml version="1.0"encoding="utf-8"?>
2.<RelativeLayout xmlns:android="/apk/res/android"
3.android:layout_width="fill_parent"
4.android:layout_height="fill_parent"
5.android:background="#eee"
6.android:orientation="vertical">
7.
8.<RelativeLayout
9.android:layout_width="fill_parent"
10.android:layout_height="45dp"
11.android:id="@+id/title"
12.android:background="@drawable/title_bar"
13.android:gravity="center_vertical"
14.>
15.<Button
16.android:id="@+id/back"
17.android:text="返回"
18.android:textColor="#ffffff"
19.android:textSize="15sp"
20.android:layout_alignParentLeft="true"
21.android:layout_centerVertical="true"
22.android:layout_width="80dp"
23.android:layout_height="wrap_content"
24.android:background="@drawable/title_btn_back"
25.android:onClick="goBack"
26./>
27.<TextView
28.android:layout_width="wrap_content"
29.android:layout_height="wrap_content"
30.android:layout_centerInParent="true"
31.android:text="管理"
32.android:textSize="20sp"
33.android:textColor="#ffffff"/>
34.<TextView
35.android:id="@+id/userName"
36.android:text="用户名"
37.android:textColor="#ffffff"
38.android:textSize="15sp"
39.android:layout_alignParentRight="true"
40.android:layout_centerInParent="true"
41.android:layout_width="wrap_content"
42.android:layout_height="wrap_content"
43.android:background="@drawable/title_btn_right"
44./>
45.</RelativeLayout>
46.
47.<RelativeLayout
48.android:id="@+id/layout_socket_name"
49.android:layout_below="@id/title"
50.android:layout_width="fill_parent"
51.android:layout_height="wrap_content"
52.android:layout_marginTop="14dp"
53.android:gravity="center_vertical"
54.android:clickable="true"
55.android:background="@drawable/preference_single_item">
56.<TextView
57.android:id="@+id/socket_name"
58.android:layout_width="wrap_content"
59.android:layout_height="wrap_content"
60.android:layout_centerHorizontal="true"
61.android:padding="8dp"
62.android:text="无线插座"
63.android:textSize="17sp"
64.android:textColor="#000"/>
65.
66.</RelativeLayout>
67.<RelativeLayout
68.android:layout_below="@id/layout_socket_name"
69.android:layout_width="fill_parent"
70.android:layout_height="wrap_content"
71.android:layout_marginTop="14dp"
72.android:gravity="center_horizontal"
73.android:background="@drawable/preference_single_item">
74.
75.<ImageView
76.android:id="@+id/socket_iv0"
77.android:layout_width="wrap_content"
78.android:layout_height="wrap_content"
79.android:paddingTop="8dp"
80.android:paddingLeft="8dp"
81.android:src="@drawable/jack0_close"
82.android:clickable="true"
83.android:onClick="Manage"
84./>
85.<ImageView
86.android:id="@+id/socket_iv2"
87.android:layout_width="wrap_content"
88.android:layout_height="wrap_content"
89.android:layout_toRightOf="@id/socket_iv0"
90.android:paddingTop="8dp"
91.android:paddingRight="8dp"
92.android:src="@drawable/jack2_close"
93.android:clickable="true"
94.android:onClick="Manage"
95./>
96.<ImageView
97.android:id="@+id/socket_iv1"
98.android:layout_width="wrap_content"
99.android:layout_height="wrap_content"
100.a ndroid:layout_below="@id/socket_iv0"
101.a ndroid:paddingBottom="8dp"
102.a ndroid:paddingLeft="8dp"
103.a ndroid:src="@drawable/jack1_close"
104.a ndroid:clickable="true"
105.a ndroid:onClick="Manage"
106./>
107.<ImageView
108.a ndroid:id="@+id/socket_iv3"
109.a ndroid:layout_width="wrap_content"
110.a ndroid:layout_height="wrap_content"
111.a ndroid:layout_below="@id/socket_iv2"
112.a ndroid:layout_toRightOf="@id/socket_iv1"
113.a ndroid:paddingBottom="8dp"
114.a ndroid:paddingRight="8dp"
115.a ndroid:src="@drawable/jack3_close"
116.a ndroid:clickable="true"
117.a ndroid:onClick="Manage"
118./>
119.
120.</RelativeLayout>
121.
122.<RelativeLayout
123.a ndroid:layout_width="fill_parent"
124.a ndroid:layout_height="wrap_content"
125.a ndroid:layout_marginTop="14dp"
126.a ndroid:gravity="center_vertical"
127.a ndroid:clickable="true"
128.a ndroid:layout_alignParentBottom="true"
129.a ndroid:background="@drawable/preference_single_item"> 130.<TextView
131.a ndroid:id="@+id/socket_id"
132.a ndroid:layout_width="wrap_content"
133.a ndroid:layout_height="wrap_content"
134.a ndroid:layout_centerHorizontal="true"
135.a ndroid:padding="8dp"
136.a ndroid:text="无线插座"
137.a ndroid:textSize="17sp"
138.a ndroid:textColor="#000"/>
139.
140.</RelativeLayout>
141.
142.</RelativeLayout>
布局效果图如下所示:
代码中用了Apache-Mina库文件,当然也可以不使用。
MainActivity.java代码如下[java]view plaincopyprint?
1.package com.anobodykey.remotecontrol;
2.
3.import java.io.IOException;
4.import .InetSocketAddress;
5.
6.import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
7.import org.apache.mina.core.session.IoSession;
8.import org.apache.mina.filter.codec.ProtocolCodecFilter;
9.import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
10.import org.apache.mina.transport.socket.SocketAcceptor;
11.import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
12.
13.import android.app.Activity;
14.import android.app.AlertDialog;
15.import android.content.DialogInterface;
16.import android.content.DialogInterface.OnClickListener;
17.import android.os.Bundle;
18.import android.view.KeyEvent;
19.import android.view.Menu;
20.import android.view.View;
21.import android.widget.ImageView;
22.import android.widget.Toast;
23.
24.public class MainActivity extends Activity {
25.
26.private static final int JACK_NUMBER = 4;
27.SocketAcceptor acceptor;
28.private int[] jacks_id =
29.{
30.R.id.socket_iv0, R.id.socket_iv1,
31.R.id.socket_iv2, R.id.socket_iv3,
32.};//插孔控件id
33.private int[] jacks_status =
34.{
35.R.drawable.jack0_close, R.drawable.jack0_open,
36.R.drawable.jack1_close, R.drawable.jack1_open,
37.R.drawable.jack2_close, R.drawable.jack2_open,
38.R.drawable.jack3_close, R.drawable.jack3_open,
39.};//插孔状态图片资源
40.private ImageView[] jackViews = null;
41.static public int iterator = 0; //插孔遍历器
42.static public boolean[] status = null;//插座状态
43.@Override。